mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-06 09:06:51 +08:00
Add tests for CountChar (#3334)
This commit is contained in:
@ -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();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user