问答题
下列给定程序中,函数fun的功能是:将s所指字符串中最后一次出现的与t1所指字符串相同的子串替换成t2所指字符串,所形成的新串放在w所指的数组中。要求t1和t2所指字符串的长度相同。 例如,当8所指字符串中的内容为“abcdab. fabc”,t1所指串中的内容为“ab”,t2所指子串中的内容为“99”时,结果在w所指的数组中的内容应为 “abcdabt99c”。 请改正程序中的错误,使它能得出正确的结果。 注意:不要改动main函数,不得增行或删行,也不得更改程序的结构! 试题程序:#include < conio.h >#include < stdio.h >#include < string.h >void fun(char *s,char t1,chart2,char w){ char *p,*r,*a; strcpy(w,s);/*********found*********/ while(w) { p=W; r=t1; while(*r)/*********found*********/ 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[1 00],t2[100],w[100]; printf("/nPlease enterstring S:"); Scanf("%s",s); printf("/nPlease entersubstring t1:"); scanf("%s",t1); print f("/nPlease entersubstring t2:"); scanf("%s",t2); if(str1en(t1)==str1en(t2)) { fun(s,t1,t2,w); printf ("/nThe resultis:%s、n",w); } else { printf("/nError:str1en(ti)!=str1en(t2)/n"); }}
【正确答案】正确答案:(1)while(*w) (2)if(*r== *p)
【答案解析】解析:(1)此处要判断的是值的真假,而不是地址,所以改为while(*w)。 (2)C语言中关键字区分大小写,只需运行程序,就可以根据错误提示找到。