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

题目描述

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

输入描述

输入一行字符串,能够有空格

输出描述

统计其中英文字符,空格字符,数字字符,其余字符的个数

输入例子

1qazxsw23 edcvfr45tgbn hy67uj m,ki89ol.\\/;p0-=\\][

输出例子

26
3
10
12

算法实现

import java.util.Scanner;

/**
 * All Rights Reserved !!!
 */
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
//        Scanner scanner = new Scanner(Main.class.getClassLoader().getResourceAsStream("data.txt"));
        while (scanner.hasNext()) {
            String input = scanner.nextLine();
            System.out.print(count(input));
        }

        scanner.close();
    }

    private static String count(String s) {
        int[] result = new int[4];

        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);

            if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z') {
                result[0]++;
            } else if (c == ' ') {
                result[1]++;
            } else if (c >= '0' && c <= '9') {
                result[2]++;
            } else {
                result[3]++;
            }
        }

        StringBuilder builder = new StringBuilder();
        for (int i : result) {
            builder.append(i).append('\n');
        }

        return builder.toString();
    }
}
相关文章
相关标签/搜索