请补充函数fun(),该函数的功能是:返回字符数组中指定子符的个数,指定字符从键盘输入。 注意:部分源程序给出如下。 请勿改动主函数main和其他函数中的任何内容,仅在函数fun()的横线上填入所编写的若干表达式或语句。 试题程序:

admin2010-05-05  11

问题 请补充函数fun(),该函数的功能是:返回字符数组中指定子符的个数,指定字符从键盘输入。
   注意:部分源程序给出如下。
   请勿改动主函数main和其他函数中的任何内容,仅在函数fun()的横线上填入所编写的若干表达式或语句。
   试题程序:
   #include <stdio.h>
   #define N 80
   int fun (char s[],char ch)
   {
      int i=0, n=0;
   while(【  】)
   {
       if(【  】)
             n++;
       i++;
   }
     【  】;
   }
   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);
   }

选项

答案s[i]或者s[i]!= ’\0’ s[i]=ch return n

解析 第一空:while循环的条件是当前参加比较的字符不为‘\0’,即还没有到字符串的最后一个字符。第二空:如果当前字符等于指定字符,则统计个数的变量n加1。第三空:函数要求返回字符数组中指定字符的个数,所以函数要返回 n。
转载请注明原文地址:https://kaotiyun.com/show/5XID777K
0

最新回复(0)