上代码:
#include <stdio.h>
int main()
{
int letters=0,space=0,digit=0,other=0; //计数
char c; //一个字符
while((c=getchar())!='\n') //使用getchar()连续输入字符时经常使用的一个判断条件(要记住)。运算级别低时要加括号改变运算顺序。
{
if(c>='a' && c<='z' || c>='A' && c<='Z') //关系运算符的综合运算,注意运算级别。
letters++; //符合条件,计数器加1。
else if(c==' ') //“==”不要写成赋值运算符,否则表达式结果永远为“真”。
space++;
else if(c>='0' && c<='9') //对字符的判断,要交‘ ’限定。
digit++;
else
other++;
}
printf("letters=%d\nspace=%d\ndigit=%d\nother=%d\n",letters,space,digit,other);
return 0;
}
char c;
while((c=getchar())!=’\n’)
这是连续输入字符常常使用的一个判断条件,最好记住