Incorporate the class Alphabetical from string package

This commit is contained in:
JackZeled0n 2020-10-23 20:30:46 -06:00
parent 56a611a9e1
commit f4f0dfd2d3
2 changed files with 33 additions and 0 deletions

View 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;
}
}

View 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");
}
}