题解 | #统计字符#

统计字符

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);
    }
}

全部评论

相关推荐

头像
不愿透露姓名的神秘牛友
05-24 12:16
点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务