输入一行字符,分别统计出包含英文字母、空格、数字和其它字符的个数。

思路:这题还是比较简单的,直接遍历判断即可

 public static void main(String[] args) {
       int strCount = 0;
       int blankCount = 0;
       int numCount = 0;
       int otherCount = 0;
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()){
            String str = scanner.nextLine();
            char[] chars = str.toCharArray();
            for (char c : chars) {
                if((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')){
                    strCount++;
                }
                else if(c == ' '){
                    blankCount++;
                }
                else if((c >= '0' && c <= '9')){
                    numCount++;
                }
                else {
                    otherCount++;
                }
            }
            System.out.println(strCount);
            System.out.println(blankCount);
            System.out.println(numCount);
            System.out.println(otherCount);
        }

    }

 

上一篇:LeetCode_443. String Compression


下一篇:Shell字符串截取(非常详细)