java题目统计字符

描述

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

本题包含多组输入。

数据范围:输入的字符串长度满足 1 \le n \le 1000 \1n1000 

输入描述:

输入一行字符串,可以有空格

输出描述:

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

示例1

输入:
1qazxsw23 edcvfr45tgbn hy67uj m,ki89ol.\\/;p0-=\\][
输出:
26
3
10
12

 

 

 1 import java.io.*;
 2 import java.util.*;
 3 
 4 public class Main{
 5     public static void main(String[] args) throws IOException {
 6         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
 7         String str = "";
 8         while((str=br.readLine()) != null){
 9             char[] ch = str.toCharArray();
10             int ziuf=0;
11             int space=0;
12             int number=0;
13             int other=0;
14             for (int i = 0; i < ch.length; i++) {
15                 if((ch[i] >= 'a' && ch[i] <= 'z' ) || (ch[i] >= 'A' && ch[i] <= 'Z' )) {
16                     ziuf++;
17                     continue;
18                 } else if(ch[i] == ' ') {
19                     space++;
20                     continue;
21                 } else if(ch[i] >='0' && ch[i] <='9') {
22                     number++;
23                     continue;
24                 } else {
25                     other++;
26                 }
27             }
28             System.out.println(ziuf + "\r\n" + space + "\r\n" + number + "\r\n" + other);
29         }
30     }
31 }

 

posted @ 2022-03-05 16:55  海漠  阅读(43)  评论(0编辑  收藏  举报