JavaAlgorithms/strings/Alphabetical.java

34 lines
954 B
Java
Raw Normal View History

package strings;
/**
2020-10-24 18:23:28 +08:00
* Alphabetical order is a system whereby character strings are placed in order based on the
* position of the characters in the conventional ordering of an alphabet. Wikipedia:
* https://en.wikipedia.org/wiki/Alphabetical_order
*/
class Alphabetical {
2020-10-24 18:23:28 +08:00
public static void main(String[] args) {
assert !isAlphabetical("123abc");
assert isAlphabetical("aBC");
assert isAlphabetical("abc");
assert !isAlphabetical("xyzabc");
assert isAlphabetical("abcxyz");
}
2020-10-24 18:23:28 +08:00
/**
* 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;
}
}
2020-10-24 18:23:28 +08:00
return true;
}
}