編程題
請編寫函數(shù)FUN,它的功能是:求出SS所指字符串中指定字符的個數(shù),并返回此值。
例如,若輸入字符串123412132,輸入字符1,則輸出3。
注意:部分源程序給出如下。
請勿改動主函數(shù)main和其他函數(shù)中的任何內(nèi)容,僅在函數(shù)fun的花括號中填入所編寫的若干語句。
試題程序:#include
#include
#include
#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;
}