问题遇到的现象和发生背景
c语言,vs2022,输入字符串统计字母和数字的个数,可以运行,但是得出的结果不对
用代码块功能插入代码,请勿粘贴截图
#include<stdio.h>
int main()
{
char a[80];
int i = 0;
char j=0;
int letter=0, sum=0, space=0, other=0;
for (j= 0; j < 80; j++)
{
scanf("%c", &a[j]);
if (a[j] == '\n')
break;
}
for (i = 0; i < j; i++)
{
if (a[i] > '0' && a[i] < '9')
sum++;
else
if (a[i] > 'a' && a[i] < 'z' || a[i] > 'A' && a[i] < 'Z')
letter++;
else
if (a[i] == ' ')
space++;
else
other++;
}
printf("字母有:%d个\n数字有:%d个\n空格有:%d个\n其他字符有:%d个", letter, sum, space, other);
return 0;
}
运行结果及报错内容
输入:i am a student 时输出字母有9个,数字0个,空格有3个,其他字符有2个
我的解答思路和尝试过的方法
用for循环输入一个字符串,再用for循环判断每一个字符的类型
我想要达到的结果
能正确得出结果