1. 程式人生 > 其它 >java題目統計字元

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 }