统计各类字符个数

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

 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
");
 8     while( (c=getchar()) != '
' )
 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
", letters, digits, spaces, others);
20     return 0;
21 }
View Code

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

亲爱的读者:如果觉得本文对你有所帮助,请点击推荐,分享给其他人!
原文地址:https://www.cnblogs.com/zhuangwei/p/5276737.html