我试图编写一个函数,它将给定字符串s中的所有字符串s1替换为s2。然而,我不知道为什么我的程序停止在该行* p = 0在该替换函数没有任何错误报告? @@调试字符串替换函数C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void replace(char * s, char * s1, char * s2) {
char * p; int l=strlen(s2);
while ((p=strstr(s,s1))) {
*p=0;
p+=l;
strcat(s,s2);
strcat(s,p);
}
}
int main(void) {
char *s=(char *)"cmd=ls+-la&abc=xyz";
replace (s, "+", " ");
printf("%s", s);
return EXIT_SUCCESS;
}
看看“倒楣的画家算法“关于您使用的'strcat':http://en.wikipedia.org/wiki/Schlemiel_the_Painter%27s_algorithm – 2011-05-31 17:44:17