Incorporate the class Alphabetical from string package
This commit is contained in:
parent
56a611a9e1
commit
f4f0dfd2d3
20
src/main/java/com/string/Alphabetical.java
Normal file
20
src/main/java/com/string/Alphabetical.java
Normal file
@ -0,0 +1,20 @@
|
||||
package com.string;
|
||||
|
||||
public class Alphabetical {
|
||||
|
||||
/**
|
||||
* Check if a string is alphabetical order or not
|
||||
*
|
||||
* @param s a string
|
||||
* @return {@code true} if given string is alphabetical order, otherwise {@code false}
|
||||
*/
|
||||
public static boolean isAlphabetical(String s) {
|
||||
s = s.toLowerCase();
|
||||
for (int i = 0; i < s.length() - 1; ++i) {
|
||||
if (!Character.isLetter(s.charAt(i)) || !(s.charAt(i) <= s.charAt(i + 1))) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
13
src/test/java/com/string/AlphabeticalTest.java
Normal file
13
src/test/java/com/string/AlphabeticalTest.java
Normal file
@ -0,0 +1,13 @@
|
||||
package com.string;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class AlphabeticalTest extends Alphabetical {
|
||||
|
||||
@Test
|
||||
void testAlphabetical() {
|
||||
Assertions.assertEquals(true, isAlphabetical("abc"), "The string is in order");
|
||||
Assertions.assertEquals(false, isAlphabetical("testing"), "The string is not in order");
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user