ECNU 2537 统计字符

ECNU 2537 统计字符

链接

https://acm.ecnu.edu.cn/problem/2537

题目

单点时限: 2.0 sec

内存限制: 256 MB

输入一行字符,分别统计其中英文字母、空格、数字和其他字符的个数。

输入格式
输入一个整数 ,表示有几组数据

接下来有 行,每行字符不超过 个

输出格式
对于每行字符输出其中

1 英文字母(大小写都算)的个数

2 数字的个数

3 其他字符的个数

样例
input
2
q2 e2
qweqrwwerr232424fwetetg===2342gdsg3.,/-=@321
output
character:2
number:2
others:1
character:21
number:14
others:9

思路

asc码进行判断,数字,字母,其他分类统计,最后输出即可,没有空格的检索简单多了。

代码

 public static void fun() {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    String temp = sc.nextLine();
    for (int i = 0; i < n; i++) {

      String line = sc.nextLine();
      StringBuffer sb = new StringBuffer(line);
      int ch = 0;
      int num = 0;
      int other = 0;
      for (int j = 0; j < sb.length(); j++) {
        char use = sb.charAt(j);
        if (use >= '0' && use <= '9') {
          num++;
        } else if (use >= 'a' && use <= 'z') {
          ch++;
        } else if (use >= 'A' && use <= 'Z') {
          ch++;
        } else {
          other++;
        }
      }

      System.out.println("character:" + ch);
      System.out.println("number:" + num);
      System.out.println("others:" + other);

    }

  }

原文地址:https://www.cnblogs.com/blogxjc/p/14382830.html