diff --git a/src/main/java/com/thealgorithms/others/CountChar.java b/src/main/java/com/thealgorithms/others/CountChar.java index 11876e409..5a78c0c17 100644 --- a/src/main/java/com/thealgorithms/others/CountChar.java +++ b/src/main/java/com/thealgorithms/others/CountChar.java @@ -1,26 +1,15 @@ package com.thealgorithms.others; -import java.util.Scanner; - public class CountChar { - public static void main(String[] args) { - Scanner input = new Scanner(System.in); - System.out.print("Enter your text: "); - String str = input.nextLine(); - input.close(); - System.out.println( - "There are " + CountCharacters(str) + " characters." - ); - } - /** * Count non space character in string * * @param str String to count the characters * @return number of character in the specified string */ - private static int CountCharacters(String str) { + + public static int CountCharacters(String str) { return str.replaceAll("\\s", "").length(); } } diff --git a/src/test/java/com/thealgorithms/others/CountCharTest.java b/src/test/java/com/thealgorithms/others/CountCharTest.java new file mode 100644 index 000000000..9e9972c68 --- /dev/null +++ b/src/test/java/com/thealgorithms/others/CountCharTest.java @@ -0,0 +1,17 @@ +package com.thealgorithms.others; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class CountCharTest { + + @Test + void testCountCharacters(){ + String input = "12345"; + int expectedValue = 5; + + assertEquals(expectedValue, CountChar.CountCharacters(input)); + } + +} \ No newline at end of file