File tree Expand file tree Collapse file tree 2 files changed +19
-13
lines changed
main/java/com/thealgorithms/others
test/java/com/thealgorithms/others Expand file tree Collapse file tree 2 files changed +19
-13
lines changed Original file line number Diff line number Diff line change 11package com .thealgorithms .others ;
22
3- import java .util .Scanner ;
4-
53public class CountChar {
64
7- public static void main (String [] args ) {
8- Scanner input = new Scanner (System .in );
9- System .out .print ("Enter your text: " );
10- String str = input .nextLine ();
11- input .close ();
12- System .out .println (
13- "There are " + CountCharacters (str ) + " characters."
14- );
15- }
16-
175 /**
186 * Count non space character in string
197 *
208 * @param str String to count the characters
219 * @return number of character in the specified string
2210 */
23- private static int CountCharacters (String str ) {
11+
12+ public static int CountCharacters (String str ) {
2413 return str .replaceAll ("\\ s" , "" ).length ();
2514 }
2615}
Original file line number Diff line number Diff line change 1+ package com .thealgorithms .others ;
2+
3+ import org .junit .jupiter .api .Test ;
4+
5+ import static org .junit .jupiter .api .Assertions .*;
6+
7+ class CountCharTest {
8+
9+ @ Test
10+ void testCountCharacters (){
11+ String input = "12345" ;
12+ int expectedValue = 5 ;
13+
14+ assertEquals (expectedValue , CountChar .CountCharacters (input ));
15+ }
16+
17+ }
You can’t perform that action at this time.
0 commit comments