|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- #if(0)
- 第十七题:
- 题目:输入一行字符,分别统计出其中英文字母、空格、数字和其他字符的个数。
- #endif
- #include<stdio.h>
- int main()
- {
- char c;
- int num=0,space=0,letter=0,other=0;
- printf("请输入字符:");
- c=getchar();
- while (c != '\n')
- {
- if(c>='0'&&c<='9')
- num++;
- else if(c>='a'&&c<='z')
- letter++;
- else if(c>='A'&&c<='Z')
- letter++;
- else if(c==' ')
- space++;
- else
- other++;
- }
- printf("数字的个数为%d\n",num);
- printf("字母的个数为%d\n",letter);
- printf("空格的个数为%d\n",space);
- printf("其他字符的个数为%d\n",other);
- return 0;
- }
复制代码
有大佬能指点一下为什么最后的几个printf函数没有运行的原因吗
因为死循环了。while循环内没有修改过变量c的值,所以死循环了
- #if(0)
- 第十七题:
- 题目:输入一行字符,分别统计出其中英文字母、空格、数字和其他字符的个数。
- #endif
- #include<stdio.h>
- int main()
- {
- char c;
- int num=0,space=0,letter=0,other=0;
- printf("请输入字符:");
- c=getchar();
- while (c != '\n')
- {
- if(c>='0'&&c<='9')
- num++;
- else if(c>='a'&&c<='z')
- letter++;
- else if(c>='A'&&c<='Z')
- letter++;
- else if(c==' ')
- space++;
- else
- other++;
- c=getchar();//读取下一个字符,修改c值
- }
- printf("数字的个数为%d\n",num);
- printf("字母的个数为%d\n",letter);
- printf("空格的个数为%d\n",space);
- printf("其他字符的个数为%d\n",other);
- return 0;
- }
复制代码
|
|