题目描述 输入一行字符,分别统计其中英文字母、空格、数字和其他字符的个数,分行输出该结果。
输入 一行字符,可以包含字母、数字、空格、标点等符号
输出 分行输出大小写英文字母、空格、数字和其他字符的个数。
如:
characters=字母个数
spaces=空格个数
numbers=数字个数
others=其他字符个数
样例输入
My input123 @%chars.
样例输出
characters=12
spaces=2
numbers=3
others=3
整个题目理清楚大概的思路,其实不难,首先对于一行字符进行getchar取值,
判定其至’\n’打止,
字母的判定条件则是’A’~‘Z’
空格则是‘ ’ ,记住’ ‘里边得空一格
数字也很简单 ‘0’~‘9’
其余的就用else
所有判定条件因为是char型变量进行判定,都得加’ '。
#include<stdio.h>
int main()
{ char c;
int characters=0,spaces=0,numbers=0,others=0; //对每行代码进行赋值
while ((c=getchar())!='\n') //c进行取值,直到有换行就停止取值了
{
if((c>='a'&&c<='z')||(c>='A'&&c<='Z')) characters++; //遇到字母,+1
else if(c==' ') spaces++; //遇到空格,+1
else if(c>='0'&&c<='9') numbers++; //遇到数字,+1
else others++; //其他的,+1
}
printf("characters=%d\nspaces=%d\nnumbers=%d\nothers=%d",characters,spaces,numbers,others); //输出,各个符号记得输入清楚
return 0;
}