编程题 请编写函数FUN,它的功能是:求出SS所指字符串中指定字符的个数,并返回此值。
例如,若输入字符串123412132,输入字符1,则输出3。
注意:部分源程序给出如下。
请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
试题程序:#include <conio.h>
#include <stdio.h>
#include <string.h>
#define M 81
int fun(char *ss, char c)
{
}
main()
{
char a[M], ch;
FILE *out;
printf("\nPlease enter a string:");
gets(a);
printf("\nPlease enter a char:");
ch = getchar();
printf("\nThe number of the char is: %d\n", fun(a, ch));
out=fopen ("out.dat", "w");
strcpy(a, "The number of the char is: ");
fprintf(out, "%d", fun(a, ' '));
fclose (out );
}
答案是:int fun(char *ss,char c)
{
int n=0;
while(*ss)
{
if(*ss==c)
n++;
ss++;
}
return n;
}