输入一行字符,分别统计其中英文字母、空格、数字和其他字符的个数。
样例:
输入 abcd12 def @56xyz!
输出 英文字母10个、空格2个、数字4个、其他字符2个。
#include<stdio.h>
int main()
{
char ch;
int charNum=0,spaceNum=0,numNum=0,otherNum=0;//设4个变量,为以后记录个数
ch=getchar();//得到一个字符
while(ch!='\n'){//当输入换行符即敲回车的时候中止循环
if(ch>='0'&&ch<='9'){//判断该字符是否为数字
numNum++;}
else if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z')){//判断该字符是否为英文字母
charNum++;}
else if(ch==' '){//判断空格的个数
spaceNum++;}
else{
otherNum++;
}
ch=getchar();
}
printf("英文字母%d个、空格%d个、数字%d个、其他字符%d个。",charNum,spaceNum,numNum,otherNum);
return 0;
}