C语言编程>第十一周 ⑦ 请编写函数fun,它的功能是:求出str所指字符串中指定字符的个数,并返回此值。

例题:请编写函数fun,它的功能是:求出str所指字符串中指定字符的个数,并返回此值。

例如,若输入字符串asdfasdf,输入字符a,则输出2。
请勿改动主函数main与其它函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。

代码如下:

#include
#include
#include
#define N 100
int fun(char*str,char c)
{
     
	int n=0;
	while(*str)
	{
     
		if(*str==c)
		n++;
		str++;
	}
	return n;
}
main()
{
     
	char s[N],ch;
	FILE*out;
	printf("\nPlease enter a string:");
	gets(s);
	printf("\nPlease enter a char:");
	ch=getchar();
	printf("\nThe number of the char is:%d\n",fun(s,ch));
	out=fopen("outfile.dat","w");
	strcpy(s,"The number of the char is:");
	fprintf(out,"%d",fun(s,'a'));
	fclose(out);
}

输出运行窗口如下:
C语言编程>第十一周 ⑦ 请编写函数fun,它的功能是:求出str所指字符串中指定字符的个数,并返回此值。_第1张图片

越努力越幸运!
加油,奥力给!!!

你可能感兴趣的:(笔记)