統計各類字符個數,統計字符個數
1 //題目:輸入一行字符,分別統計其中英文字母、空格、數字和其它字符的個數
2 //程序分析:利用while語句,條件為輸入的字符不為'\n'。
3
4 #include<stdio.h>
5
6 int main()
7 {
8 char c;
9 int letters = 0, spaces = 0, digits = 0, others = 0;
10 printf("Please input some characters\n");
11 while( (c=getchar()) != '\n' )
12 {
13 if( (c>='a'&&c<='z') || (c>='A'&&c<='Z') )
14 letters++;
15 else if( c>='0'&&c<='9' )
16 digits++;
17 else if( c==' ' )
18 spaces++;
19 else
20 others++;
21 }
22 printf("letters=%d,digits=%d,spaces=%d,others=%d\n", letters, digits, spaces, others);
23 return 0;
24 }