C语言:输入一行字符串统计出英文字母,空格,数字和其他字符的个数

题目要求
输入一行字符串统计出英文字母,空格,数字和其他字符的个数。
程序分析
要统计英文字母,空格,数字和其他字符的个数,则要遇到他们加一。
核心代码如下:

while ((c=getchar())!='\n')
	{
		if ((c >= 'a' && c <= 'z')||(c >= 'A' && c <= 'Z'))
		{
			letters++;
		}
		else if (' ' == c)
		{
			space++;
		}
		else if (c >= '0' && c <= '9')
		{
			digit++;
		}
		else
		{
			other++;
		}
	}

全部代码:

#include 
#include 
int main()
{
	char c;
	int letters=0;
	int space=0;
	int digit=0;
	int other=0;
	printf ("请输入一行字符:>");

	while ((c=getchar())!='\n')
	{
		if ((c >= 'a' && c <= 'z')||(c >= 'A' && c <= 'Z'))
		{
			letters++;
		}
		else if (' ' == c)
		{
			space++;
		}
		else if (c >= '0' && c <= '9')
		{
			digit++;
		}
		else
		{
			other++;
		}
	}
	printf ("字母的个数:>%d\n空格的个数:>%d\
			\n数字的个数:>%d\n其他字符的个数:>%d\n",\
		letters,space,digit,other);
	system ("pause");
	return 0;
}

展示一下运行结果:
C语言:输入一行字符串统计出英文字母,空格,数字和其他字符的个数_第1张图片

你可能感兴趣的:(C语言:输入一行字符串统计出英文字母,空格,数字和其他字符的个数)