Merge pull request #1925 from JackZeled0n/Development

Incorporate the class Alphabetical from string package
This commit is contained in:
Du Yuanchao 2020-10-24 18:43:23 +08:00 committed by GitHub
commit 459188428a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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");
}
}