From f4f0dfd2d378fcff8fccb611daf3fdf3d35c6a89 Mon Sep 17 00:00:00 2001 From: JackZeled0n Date: Fri, 23 Oct 2020 20:30:46 -0600 Subject: [PATCH] Incorporate the class Alphabetical from string package --- src/main/java/com/string/Alphabetical.java | 20 +++++++++++++++++++ .../java/com/string/AlphabeticalTest.java | 13 ++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 src/main/java/com/string/Alphabetical.java create mode 100644 src/test/java/com/string/AlphabeticalTest.java 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