2013年计算机二级C语言上机题库十二_第2页

考试站(www.examzz.com)   【考试站:中国教育考试第一门户】   2013年4月12日
 改错题
  下列给定程序中函数FUN的功能是:求出在字符串中最后一次出现的子符串的地址,通过函数值返回,在主函数中输出从此开始的字符串;若未找到,则函数值为NULL。例如,当字符串中的内容为abcdabfabcdx,t中的内容为ab时,输出结果应是:abcdx。当字符串中的内容为abcdabfabcdx,t中的内容为abd时,则程序输出未找到信息:not be found!。
  请改正程序中的错误,使它能得出正确结果。
  注意:不要改动MAIN函数,不得增行或删行,也不要更改程序的结构!
  试题程序:#include <conio.h>
  #include <stdio.h>
  #include <string.h>
  char *fun(char *s, char *t)
  {
  char *p, *r, *a;
  /********found********/
  a = NuLL;
  while (*s)
  {
  p = s;
  r = t;
  while (*r)
  /********found********/
  if (r == p)
  {
  r++;
  p++;
  }
  else
  {
  break;
  }
  if (*r == '\0')
  a = s;
  s++;
  }
  return a;
  }
  main()
  {
  char s[100], t[100], *p;
  printf("\nplease enter string s:");
  scanf("%s", s);
  printf("\nplease enter substring t:");
  scanf("%s", t);
  p = fun(s, t);
  if (p)
  printf("\nthe result is:%s\n", p);
  else
  printf("\nnot found!\n");
  }
  第1处:a = NULL;应改为a=NULL;
  第2处:if(r==p)应改为if(*r==*p)

相关文章