2019-05-09 19:32:54 +08:00
|
|
|
package Others;
|
|
|
|
|
2017-07-08 09:12:14 +08:00
|
|
|
import java.util.Scanner;
|
|
|
|
|
|
|
|
public class CountChar {
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
2019-05-09 19:32:54 +08:00
|
|
|
Scanner input = new Scanner(System.in);
|
|
|
|
System.out.print("Enter your text: ");
|
|
|
|
String str = input.nextLine();
|
2018-01-24 23:57:30 +08:00
|
|
|
input.close();
|
2017-07-08 09:12:14 +08:00
|
|
|
System.out.println("There are " + CountCharacters(str) + " characters.");
|
|
|
|
}
|
2019-05-09 19:32:54 +08:00
|
|
|
|
2017-07-08 09:12:14 +08:00
|
|
|
/**
|
2019-10-29 12:40:39 +08:00
|
|
|
* Count non space character in string
|
|
|
|
*
|
|
|
|
* @param str String to count the characters
|
|
|
|
* @return number of character in the specified string
|
2019-05-09 19:32:54 +08:00
|
|
|
*/
|
2018-01-24 23:58:38 +08:00
|
|
|
private static int CountCharacters(String str) {
|
2019-10-29 12:40:39 +08:00
|
|
|
return str.replaceAll("\\s", "").length();
|
2019-05-09 19:32:54 +08:00
|
|
|
}
|
2017-07-08 09:12:14 +08:00
|
|
|
}
|