diff --git a/src/main/java/com/string/Alphabetical.java b/src/main/java/com/string/Alphabetical.java new file mode 100644 index 00000000..84b0f1c8 --- /dev/null +++ b/src/main/java/com/string/Alphabetical.java @@ -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; + } +} diff --git a/src/test/java/com/string/AlphabeticalTest.java b/src/test/java/com/string/AlphabeticalTest.java new file mode 100644 index 00000000..7291546a --- /dev/null +++ b/src/test/java/com/string/AlphabeticalTest.java @@ -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"); + } +} \ No newline at end of file