题目来源:计蒜客
输入一行字符,分别统计出其中英文字母、空格、数字和其他字符的个数。
一行字符
统计值
样例1
输入:
aklsjflj123 sadf918u324 asdf91u32oasdf/.';123
输出:
23 16 2 4
import java.util.*;
public class Main {
public static void main(String args[]) throws Exception {
Scanner scanner = new Scanner(System.in);
String str = scanner.nextLine();
if(str.length() != 0){
countNum(str);
}else{
System.out.println("error");
}
}
public static void countNum(String src){
char[] cha = src.toCharArray();
int eng = 0;
int blank = 0;
int num = 0;
int other = 0;
for(int i = 0;i < cha.length;i++){
if((cha[i] >= 'a' && cha[i] <= 'z') || (cha[i] >= 'A' && cha[i] <= 'Z')){
eng++;
}else if(cha[i] == ' '){
blank++;
}else if(cha[i] >= '0' && cha[i] <= '9'){
num++;
}else{
other++;
}
}
System.out.println(eng + " " + num + " " + blank + " " + other);
}
}