#include "stdio.h"
void main()
{
char ch;
int alph = 0,digit = 0, empty = 0,other =0;
printf("输入一行字符");
while((ch = getchar())!= '\n')
{
if(ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z')
alph++;
else if(ch >= '0' && ch <= '9')
digit++;
else if(ch == ' ')
empty++;
else other++;
}
printf("字母的个数为%d\n",alph);
printf("数字的个数为%d\n",digit);
printf("空格数为%d\n",empty);
printf("其它字符的个数为%d\n",other);
}
你自己看下我的和你有什么不一样啊,就是你一个地方没有用好,下面这个也有同样的效果啊,
#include "stdio.h"
void main()
{
char ch;
int alph = 0,digit = 0, empty = 0,other =0;
printf("输入一行字符");
ch = getchar();
while(ch!= '\n')
{
if(ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z')
alph++;
else if(ch >= '0' && ch <= '9')
digit++;
else if(ch == ' ')
empty++;
else other++;
ch = getchar();
}
printf("字母的个数为%d\n",alph);
printf("数字的个数为%d\n",digit);
printf("空格数为%d\n",empty);
printf("其它字符的个数为%d\n",other);
让我们一起努力好好的学习吧