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

 1 #include<stdio.h>
 2 
 3 int main()
 4 {
 5     char c;
 6     int letters = 0, spaces = 0, digits = 0, others = 0;
 7     printf("Please input some characters\n");
 8     while( (c=getchar()) != '\n' )
 9     {
10         if( (c>='a'&&c<='z') || (c>='A'&&c<='Z') )
11             letters++;
12         else if( c>='0'&&c<='9' )
13             digits++;
14         else if( c==' ' )
15             spaces++;
16         else
17             others++;
18     }
19     printf("letters=%d,digits=%d,spaces=%d,others=%d\n", letters, digits, spaces, others);
20     return 0;
21 }
View Code

分析:利用while语句,条件为输入的字符不为'\n'。

 posted on 2016-03-14 18:47  tostring_char  阅读(649)  评论(1编辑  收藏  举报