33,311
社区成员
发帖
与我相关
我的任务
分享
#include<stdio.h>
int main()
{
int n = 0;
int x = 0;
int y = 0;
int c;
while (c != '\n')
{
c = getchar();
if ((c >= 'A'&& c <= 'Z')|| (c >= 'a'&&c <= 'z'))
n++;
else if (c >= 48 && c <= 57)
x++;
else
y++;
}
printf("字母 total:%d\n", n);
printf("数字 total:%d\n", x);
printf("其他字符 total:%d\n", y);
return 0;
}
while都是一样的。不可能是while的问题;
注意遇到换行符'\n'退出循环,但是在退出循环前,已经计算了,即其他字符就已经加1;
即使不输入数字和字母,直接输入回车,得到其他字符为1,就是计算了'\n'
如此判断就不会将换行符算入其他字符了:
#include<stdio.h>
int main()
{
int n = 0;
int x = 0;
int y = 0;
int c;
while ((c = getchar()) != '\n')
{
if ((c >= 'A'&& c <= 'Z')|| (c >= 'a'&&c <= 'z'))
n++;
else if (c >= 48 && c <= 57)
x++;
else
y++;
}
printf("字母 total:%d\n", n);
printf("数字 total:%d\n", x);
printf("其他字符 total:%d\n", y);
return 0;
}