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

//C程序设计第四版(谭浩强)
//章节:第七章 用函数实现模块化程序设计
//题号:7.9
//题目:编写一个函数,由实参传来一个字符串,统计此字符串中字母、数字、空格和其他字符的个数,
//在主函数中输入字符串以及输出上述的结果。 
#include <stdio.h>
#include <string.h>
void count(char s[])
{
	int letter=0,number=0,space=0,other=0,i,len=strlen(s);
	for(i=0;i<len;i++)
	{
		if(s[i]>='a'&&s[i]<='z'||s[i]>='A'&&s[i]<='Z')
			letter++;
		else if(s[i]>='0'&&s[i]<='9')
			number++;
		else if(s[i]==' ')
			space++;
		else
			other++;
	}
	printf("letter:%d\nnumber:%d\nspace:%d\nother:%d\n",letter,number,space,other);
 } 
int main()
{
	char s[80];
	printf("input string:\n");
	gets(s);
	count(s);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_44589540/article/details/86621946