填空题
请补充函数fun(),该函数的功能是:返回字符数组中指定子符的个数,指定字符从键盘输入。
注意:部分源程序给出如下。
请勿改动主函数main和其他函数中的任何内容,仅在函数fun()的横线上填入所编写的若干表达式或语句。
试题程序:
#include <stdio.h>
#define N 80
int fun (char s[],char ch)
{
int i=0, n=0;
while({{U}} 【1】 {{/U}})
{
if({{U}} 【2】 {{/U}})
n++;
i++;
}
{{U}} 【3】 {{/U}};
}
main ( )
{
int n;
char str[N], ch;
clrscr ();
printf ("/nInput a string: /n");
gets (str);
printf ("/nInput a charactor: /n" ;
scanf ("%c", &ch);
n=fun (str, ch);
printf("/nnumber of %c:%d", ch, n);
}
【正确答案】
1、[1] s[i]或者s[i]!= '/0' [2] s[i]=ch [3] return n
【答案解析】[解析] 填空1:while循环的条件是当前参加比较的字符不为‘/0’,即还没有到字符串的最后一个字符。填空2:如果当前字符等于指定字符,则统计个数的变量n加1。填空3:函数要求返回字符数组中指定字符的个数,所以函数要返回 n。