看了半天也没看出来哪里错了,统计结果把全部输入的字符都算作其他了,不是别字母和数字
#include<stdio.h>
#include<ctype.h>
int alpha=0,num=0,el=0;
void main()
{
char a;
puts("请输入一行字符");
while(a=getchar()!="\n")
{
//if(a>="a"&&a<="z")
if(isalpha(a))
alpha++;
if(isdigit(a))
num++;
else
el++;
}
printf("字母%d,数字%d,其他%d\n",alpha,num,el);
}
解决方案
40
#include<stdio.h>
#include<ctype.h>
int main()
{
int ch;
int alpha = 0, num = 0, el = 0;
puts("请输入一行字符");
while ((ch = getchar()) != "\n") {
if(isalpha(ch))
alpha++;
else if(isdigit(ch))
num++;
else
el++;
}
printf("字母%d,数字%d,其他%d\n", alpha, num, el);
return 0;
}
1. while判断需要加一个括号: while ((ch = getchar()) != “\n”)
2. 判断字符是什么,要用if .. else if … else,假如第二个else不加,例如输入的是”a”会在判断不是数字,那么近视其他了。所以,需要加else if …
3. 建议用局部变量,假如没有需要,建议不用全局变量;从数据安全和代码维护角度上考虑。