C 实战练习题目17

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

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

实例:

 1 #include<stdio.h>
 2 int main()
 3 {
 4     char c;
 5     int letters=0,spaces=0,digits=0,others=0;
 6     printf("请输入一些字母:\n");
 7     while((c=getchar())!='\n')
 8     {
 9         if((c>='a'&&c<='z')||(c>='A'&&c<='Z'))
10             letters++;
11         else if(c>='0'&&c<='9')
12             digits++;
13         else if(c==' ')
14             spaces++;
15         else
16             others++;
17     }
18     printf("字母=%d,数字=%d,空格=%d,其他=%d\n",letters,digits,spaces,others);
19     return 0;
20 }

以上实例输出结果为:

请输入一些字母:
www.kangyifan.com 123
字母=15,数字=3,空格=1,其他=2

 感谢你的阅读,请用心感悟!希望可以帮到爱学习的你!!分享也是一种快乐!!!请接力。。。

点击查看原文,谢谢!

posted @ 2020-05-29 12:00  C语言自学网  阅读(239)  评论(0编辑  收藏  举报