题解 | #统计字符#
统计字符
http://www.nowcoder.com/practice/539054b4c33b4776bc350155f7abd8f5
Java 利用正则表达式
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(sc.hasNextLine()) {
String str = sc.nextLine();
String str1 = str.replaceAll("[^a-zA-Z]", "");
String str2 = str.replaceAll("[^ ]", "");
String str3 = str.replaceAll("[^0-9]", "");
String str4 = str.replaceAll("[a-zA-Z0-9 ]", "");
System.out.println(str1.length());
System.out.println(str2.length());
System.out.println(str3.length());
System.out.println(str4.length());
}
}
}