Add tests for CountChar (#3334)
This commit is contained in:
parent
c59fc923bf
commit
c805437c0c
@ -1,26 +1,15 @@
|
|||||||
package com.thealgorithms.others;
|
package com.thealgorithms.others;
|
||||||
|
|
||||||
import java.util.Scanner;
|
|
||||||
|
|
||||||
public class CountChar {
|
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
|
* Count non space character in string
|
||||||
*
|
*
|
||||||
* @param str String to count the characters
|
* @param str String to count the characters
|
||||||
* @return number of character in the specified string
|
* @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();
|
return str.replaceAll("\\s", "").length();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
17
src/test/java/com/thealgorithms/others/CountCharTest.java
Normal file
17
src/test/java/com/thealgorithms/others/CountCharTest.java
Normal file
@ -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));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user