题解 | #统计字符#
统计字符
https://www.nowcoder.com/practice/539054b4c33b4776bc350155f7abd8f5
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
if(str.length()==0 || str.length()>1000 || "".equals(str)){ //""在前可避免空指针
return;
}
int countE = 0;
int countK = 0;
int countN = 0;
int countO = 0;
for(char c: str.toCharArray()){
if((c>='a' && c<='z') || (c>='A' && c<='Z')){
countE++;
}else if(c == ' '){
countK++;
}else if(c>='0' && c<='9'){
countN++;
}else{
countO++;
}
}
System.out.println(countE);
System.out.println(countK);
System.out.println(countN);
System.out.println(countO);
}
}