模拟试题

3773考试网计算机等级考试模拟试题正文

2014年3月全国计算机二级C语言上机模拟试题十八之2

来源:2exam.com 2013-10-30 19:58:57

解题思路: 

  第一处:函数中申请了两个内存空间,其p是存放字母字符串,t是存放非字母字符串,根

  据条件可知,p依次存放字母字符串,其位置由j来控制,所以应填:s[i]。 

  第二处:利用for循环再把t中的内容依次追加到p中,其中t的长度为k,所以应填:k。 

  第三处:字符串处理好后必须添加字符串结束符,所以应填:’\0’。 

  *************************************************** 

  给定程序MODI1.C中函数 fun 的功能是:将s所指字符串中最后一次出现的与 

  t1所指字符串相同的子串替换成t2所指字符串,所形成的新串放在w所指的数组中。 

  在此处,要求t1和t2所指字符串的长度相同。 

  例如,当s所指字符串中的内容为:"abcdabfabc",t1所指子串中的内容为: "ab",t2所指子串中的内容为:"99"时, 结果,在w所指的数组中的内容应为: 

  "abcdabf99c"。 

  请改正程序中的错误,使它能得出正确的结果。 

  注意:不要改动main函数,不得增行或删行,也不得更改程序的结构! 

  给定源程序: 

  #include  

  #include  

  int fun (char *s, char *t1, char *t2 , char *w) 

  { 

  int i; char *p , *r, *a; 

  strcpy( w, s ); 

  while ( w ) 

  { p = w; r = t1; 

  while ( *r ) 

  IF ( *r == *p ) 

  { r++; p++; } 

  else break; 

  if ( *r == ’\0’ ) a = w; 

  w++; 

  } 

  r = t2; 

  while ( *r ){ *a = *r; a++; r++; } 

  } 

  main() 

  { 

  char s[100], t1[100], t2[100], w[100]; 

  printf("\nPlease enter string S:"); scanf("%s", s); 

  printf("\nPlease enter substring t1:"); scanf("%s", t1); 

  printf("\nPlease enter substring t2:"); scanf("%s", t2); 

  if ( strlen(t1)==strlen(t2) ) 

  { fun( s, t1, t2, w); 

  printf("\nThe result is : %s\n", w); 

  } 

  else printf("\nError : strlen(t1) != strlen(t2)\n"); 

  } 

触屏版 电脑版
3773考试网 琼ICP备12003406号-1