解题思路:了解了解ASCII表,需要知道每一种字符的ASCII值,不用记忆,直接查找ASCII表即可
注意事项:注意字符串的获取,要灵活使用scanf获取字符,或直接利用gets()直接获取
参考代码:
#include<stdio.h>
#define N 200
int main()
{
char str[N];
int i=0;
int a,b,c,d;//声明变量a,b,c,d用来保存字母、数字、空格和其他字符的个数
a=0;
b=0;
c=0;
d=0;
gets(str); //从键盘获取字符串
while(str[i]!='\0')//使用while循环遍历整个字符串
{
if(str[i]>='A'&&str[i]<='Z'||str[i]>='a'&&str[i]<='z')//判断字符是否为字母
{
a++;
}
else if(str[i]>='0'&&str[i]<='9')//判断字符是否为数字
{
b++;
}
else if(str[i]==' ')//判断字符是否为空格
{
c++;
}
else//判断字符是否为其他字符
{
d++;
}
i++;
}
printf("%d %d %d %d",a,b,c,d);
return 0;
}
0.0分
2 人评分