编写一个函数,由实参传来一个字符串,统计此字符串中字母、数字、空格和其他字符的个数,在主函数中输人字符串以及输出上述的结果

编写一个函数,由实参传来一个字符串,统计此字符串中字母、数字、空格和其他字符的个数,在主函数中输人字符串以及输出上述的结果

题目解析:

该题的关键在于要能够写出各种字符统计的条件

代码示例:

#include<stdio.h>

int letter, digit, space, others;

void CountChar(char str[])
{
	int i;
	for (i = 0; str[i] != ''; i++)
	{
        //统计字母
		if ((str[i] >= 'a'&& str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z')) 
			letter++;
		else if (str[i] >= '0' && str[i] <= '9') //统计数字
			digit++;
		else if (str[i] == ' ')//统计空格
			space++;
		else
			others++;  //统计其他字符
	}
}

int main()
{
	char text[80];
	printf("input string:
");
	gets(text);
	printf("string: %s
", text);

	CountChar(text);
	printf("
letter:%d
digit:%d
space:%d
others:%d
", letter, digit, space, others);
	return 0;
}

运行结果:

编写一个函数,由实参传来一个字符串,统计此字符串中字母、数字、空格和其他字符的个数,在主函数中输人字符串以及输出上述的结果

原文地址:https://www.cnblogs.com/inta/p/13356733.html