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

考试站(www.examzz.com)   【考试站:中国教育考试第一门户】   2013年4月12日
  改错题
  下列给定程序中,函数FUN的功能是:判断字符CH是否与STR所指串中的某个字符相同:若相同,则什么也不做,若不同,则将其插在串的最后。
  请改正程序中的错误,使它能得出正确结果。
  注意:不要改动MAIN函数,不得增行或删行,也不要更改程序的结构!
  试题程序:#include <conio.h>
  #include <stdio.h>
  #include <string.h>
  /********found********/
  void fun(char str, char ch)
  {
  while (*str && *str!=ch)
  str++;
  /********found********/
  if (*str == ch)
  {
  str[0] = ch;
  /********found********/
  str[1] = '0';
  }
  }
  main() 
  {
  char s[81], c;
  printf("\n Please enter a string:\n");
  gets(s);
  printf("\n Please enter the character to search:");
  c = getchar();
  fun(s, c);
  printf("\nThe result is %s\n", s);
  }
  第1处:void fun(char str,char ch)应改为void fun(char *str,char ch)
  第2处:if(*str==ch)应改为if(*str==’\0’)
  第3处:str[1]=’0’;应改为str[1]=’\0’;或str[1]=0

相关文章