#include
int main()
{
char c;
int l=0,s=0,d=0,other=0;
printf("输出一串字符\n");
c=getchar();
while((c=getchar())!='\n')
{
if('a'<=c&&c<='z'||'A'<=c&&c<='Z')
l++;
else if('0'<=c&&c<='9')
d++;
else if(c==' ')
s++;
else
other++;
}
printf("字母个数%d\n空格个数%d\n数字个数%d\n其他字符个数%d\n",l,s,d,other);
return 0;
}
为什么这样不对?
char类型是一个字符,getchar是获取一个字符
#include <stdio.h>
int main()
{
char c;
int l = 0, s = 0, d = 0, other = 0;
printf("输出一串字符\n");
while ((c = getchar()) != '\n')
{
if ('a' <= c && c <= 'z' || 'A' <= c && c <= 'Z')
l++;
else if ('0' <= c && c <= '9')
d++;
else if (c == ' ')
s++;
else
other++;
}
printf("字母个数%d\n空格个数%d\n数字个数%d\n其他字符个数%d\n", l, s, d, other);
return 0;
}