Skip to content

Commit

Permalink
Add tests for CountChar (#3334)
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardo-ramos-moura authored Oct 12, 2022
1 parent c59fc92 commit c805437
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
15 changes: 2 additions & 13 deletions src/main/java/com/thealgorithms/others/CountChar.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
17 changes: 17 additions & 0 deletions src/test/java/com/thealgorithms/others/CountCharTest.java
Original file line number Diff line number Diff line change
@@ -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));
}

}

0 comments on commit c805437

Please sign in to comment.