7-9 统计字符 (10 分)

本题目要求从键盘读入一串字符,以回车结束。分别统计其中的英文字母、数字、空格和其他字符的数量。

输入格式:

输入在一行中给出一串字符。

输出格式:

按照样例格式输出统计好的英文字母、数字、空格和其他字符的数量。

输入样例:

w1e2r3t AGH4--#  23  %%**

输出样例:

字母:7,数字:6,空格:5,其他:7
#include<stdio.h>
int main()
{
	char ch='\0';
	int a=0,b=0,c=0,d=0;
	while(ch!='\n')
	{
		ch=getchar();
		if(ch>='a'&&ch<='z'||ch>='A'&&ch<='Z')
		{
			a++;
		}
		else if(ch>='0'&&ch<='9')
		{
			b++;
		}
		else if(ch==' ')
		{
			c++;
		}
		else if(ch=='\n')
		{
			
		}
		else
		{
			d++;
		}
	}
	printf("字母:%d,数字:%d,空格:%d,其他:%d",a,b,c,d);
		
} 

猜你喜欢

转载自blog.csdn.net/qq_51309939/article/details/118685087