Skip to content

Commit f4f0dfd

Browse files
committed
Incorporate the class Alphabetical from string package
1 parent 56a611a commit f4f0dfd

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.string;
2+
3+
public class Alphabetical {
4+
5+
/**
6+
* Check if a string is alphabetical order or not
7+
*
8+
* @param s a string
9+
* @return {@code true} if given string is alphabetical order, otherwise {@code false}
10+
*/
11+
public static boolean isAlphabetical(String s) {
12+
s = s.toLowerCase();
13+
for (int i = 0; i < s.length() - 1; ++i) {
14+
if (!Character.isLetter(s.charAt(i)) || !(s.charAt(i) <= s.charAt(i + 1))) {
15+
return false;
16+
}
17+
}
18+
return true;
19+
}
20+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.string;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.Test;
5+
6+
class AlphabeticalTest extends Alphabetical {
7+
8+
@Test
9+
void testAlphabetical() {
10+
Assertions.assertEquals(true, isAlphabetical("abc"), "The string is in order");
11+
Assertions.assertEquals(false, isAlphabetical("testing"), "The string is not in order");
12+
}
13+
}

0 commit comments

Comments
 (0)