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

程序分析:利用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,其他=%dn",letters,digits,spaces,others);
19     return 0;
20 }

以上实例输出结果为:

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

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

点击查看原文,谢谢!

内容来源于网络如有侵权请私信删除

文章来源: 博客园

原文链接: https://www.cnblogs.com/kangyifan/p/12986692.html

你还没有登录,请先登录注册
  • 还没有人评论,欢迎说说您的想法!