c语言实现变种 replace()
函数fun将字符串s中第一次出现的子字符串t1替换成字符串t2,替换后的新串放在w所指的数组中, 要求t1和t2所指向的字符串长度相同。 例如:当s所指字符串中的内容为"abcdab",t1中的内容为"ab",t2中的内容为"cc"时,结果w所指 数组中的内容应为"cccdab"。 程序有多处错误,请在答题框中写出错误的行号和修改后的内容。 1 int fun(char *s, char *t1, char *t2 , char *w) 2 { 3 int i; 4 char *p , *r, *a; 5 strcpy( w, s ); 6 while ( *w ) 7 { 8 p = w; 9 r = t1; 10 while ( *r ) 11 if ( *r == *p ) 12 { 13 r++; 14 p++; 15 } 16 else 17 break; 18 if ( *r ) 19 { 20 a = w; 21 continue; 22 } 23 w++; 24 } 25 r = t2; 26 while ( *r ) 27 { 28 *a = *r; 29 a++; 30 r++; 31 } 32 }
#include <stdio.h> #include <string.h> void 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; //2 } if (*r == ‘\0‘) //3 { a = w; break; } w++; } r = t2; // printf("%c\n", *a); while ( *r ) { *a = *r; //1 a++; r++; } } main(){ char s[50] = "abcdab"; char t1[50] = "ab"; char t2[50] = "cc"; char w[50]; // char *s = "abcdab"; // char *t1 = "ab"; // char *t2 = "cc"; // char *w; fun(s, t1, t2, w); printf("%s", w); return 0; }
相关推荐
88384957 2020-06-12
zuihaobushi 2020-04-30
xiaohouye 2020-04-20
菇星獨行 2020-04-20
qscool 2020-04-16
Joymine 2020-03-04
CosEmon 2020-03-01
GoatSucker 2020-02-15
Joymine 2020-02-11
twater000 2020-02-01
徐建岗网络管理 2020-07-28
AaronPlay 2020-06-13
ILVNMM 2020-10-26
PinkBean 2020-08-19
Seandba 2020-08-16
herohope 2020-06-10
adwen00 2020-06-09
KilluaZoldyck 2020-06-06