Skip to content

Commit c805437

Browse files
Add tests for CountChar (TheAlgorithms#3334)
1 parent c59fc92 commit c805437

File tree

2 files changed

+19
-13
lines changed

2 files changed

+19
-13
lines changed
Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,15 @@
11
package com.thealgorithms.others;
22

3-
import java.util.Scanner;
4-
53
public 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
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
}

0 commit comments

Comments
 (0)