题解 | #统计字符# 真简单

统计字符

https://www.nowcoder.com/practice/539054b4c33b4776bc350155f7abd8f5

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();

        String regex1 = "[a-zA-Z]";
        String regex2 = "[ ]";
        String regex3 = "[0-9]";
        String regex4 = "[^a-zA-Z0-9]";

        int count1 = 0;
        int count2 = 0;
        int count3 = 0;
        int count4 = 0;

        for (int i = 0; i < str.length(); i++) {
            String c = str.charAt(i) + "";

            if (c.matches(regex1)) {
                count1++;
            } else if (c.matches(regex2)) {
                count2++;
            } else if (c.matches(regex3)) {
                count3++;
            } else if (c.matches(regex4)) {
                count4++;
            }
        }

        System.out.println(count1);
        System.out.println(count2);
        System.out.println(count3);
        System.out.println(count4);
    }
}

全部评论

相关推荐

赛博小保安:不行你就找点东西继续干干直接等明年走社招吧,学历差的在秋招真的没戏。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务