题解 | #统计字符#
统计字符
http://www.nowcoder.com/practice/539054b4c33b4776bc350155f7abd8f5
简单题,调函数 或者自己判断也行
import java.util.*;
public class Main {
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
String str=sc.nextLine();
int word=0;
int space=0;
int num=0;
int other=0;
char[] c=str.toCharArray();
for(int i=0;i<c.length;i++){
if(Character.isLetter(c[i])){word++;}
else if(Character.isWhitespace(c[i])){space++;}
else if(Character.isDigit(c[i])){num++;}
else { other++;}
}
System.out.println(word);
System.out.println(space);
System.out.println(num);
System.out.println(other);
}
}
腾讯云智研发成长空间 5082人发布