str为一个字符序列。请补充函数fun(),该函数的功能是:查找str中值为x的元素,返回该字符序列中值为x的元素个数,并把这些值为x的元素下标依次保存在数组bb中。例如,在“abcdefahij”中查找‘a’,结果为:2个‘a’,下标依次为 0、6。

admin2010-05-05  32

问题 str为一个字符序列。请补充函数fun(),该函数的功能是:查找str中值为x的元素,返回该字符序列中值为x的元素个数,并把这些值为x的元素下标依次保存在数组bb中。例如,在“abcdefahij”中查找‘a’,结果为:2个‘a’,下标依次为 0、6。
   注意:部分源程序给出如下。
   请勿改动主函数main和其他函数中的任何内容,仅在函数fun()的横线上填入所编写的若干表达式或语句。
   试题程序:
    #include<stdio.h>
   #include<conio.h>
   #define N 20
   int bb[N];
   int fun(char *str,char ch)
   {
     int i=0, n=0;
     char t=ch;
     char *p=str;
     while(*p)
     {
        if (【  】)
        【  】;
        p++;
        i++;
     }
     return【  】;
   }
   main()
   {
      char str[N];
      char ch;
      int i,  j,n;
      clrscr();
      printf("***Input the original string
             ***\n");
      gets(str);
      printf("***The Original ***\n");
      puts(str);
      printf("***Input character ***\n");
      scanf("%c",&ch);
      n=fun(str,ch);
      printf("\nThe numbr of character is:
             %d\n", n);
      printf{"***The suffix of character
             ***\n");
      for(i=0;i<n;i++)
         printf("%d ",bb);
   }

选项

答案*p==t bb[n++]=I n

解析 第一空:通过指针p的移动来依次访问字符串的各个字符,如果指针p所指的字符等于待查找的字符,即表示找到了满足条件的字符。第二空:将找到的字符在字符数组中的下标值保存在数组bb中。第三空:变量n记录在字符串中找到待查找字符的个数。由main函数的调用可知函数fun()的返回值为n。
转载请注明原文地址:https://kaotiyun.com/show/cHID777K
0

相关试题推荐
最新回复(0)