先导:
至少得知道ASCII码中A为65,Z为90,a为97,z为122,若不记得也没事,直接printf("%d",'A');即可查到对应的ASCII码。数字类似。
题目:
输入一行字符,分别统计出其中英文字母、空格、数字和其他字符的个数。
代码:
#include<stdio.h>
int main(){
char c;
int letters=0,spaces=0,digits=0,others=0;//记录字母、空格、数字和其他字符的个数
while((c=getchar() )!='\n'){//c语言无法直接输入字符串,故采取输入一个字符检测一次来判断
if(c>=65&&c<=90||c>=97&&c<=122) letters++;
else if(c==' ') spaces++;
else if(c>='0'&&c<='9') digits++;
else others++;
}
printf("%d %d %d %d\n",letters,spaces,digits,others);//输出统计的对应的字符数
return 0;
}
测试:
样例输入
What are you doing? 123456
样例输出
15 4 6 1