(数组法)输入一行字符,分别统计其中包含的数字、字母和其他字符的个数

#include 
int main()
{
	char ch[81];
	int i, count1 = 0, count2 = 0, count3 = 0;

	printf("请输入一行字符:\n");
	fgets(ch, 81, stdin);

	for (i = 0; i < 81; i++)
	{
		if (ch[i] == '\0' || ch[i] == '\n')
		{
			printf("数字的个数为:%d\n", count1);
			printf("字母的个数为:%d\n", count2);
			printf("其他字符的个数为:%d\n", count3);
			break;
		}
		

		else if (ch[i] >= '0' && ch[i] <= '9')
			count1++;

		else if (ch[i] >= 'a' && ch[i] <= 'z' || ch[i] >= 'A' && ch[i] <= 'Z')
			count2++;
		else
			count3++;
	}

	return 0;
}

 

你可能感兴趣的:((数组法)输入一行字符,分别统计其中包含的数字、字母和其他字符的个数)