#include
#include
#include
char * replace(const char * str, const char * str1, const char * str2) {
char * pstr1, * pstr2, * pbuf1, * pbuf2;
char ch;
pbuf1 = NULL;
pbuf2 = NULL;
pstr1 = strstr(str, str1);
while(pstr1 != NULL) {
ch = *pstr1;
*pstr1 = 0;
if(pbuf1 == NULL) {
pbuf1 = (char *)malloc(strlen(str) + strlen(str2) + 1);
pbuf1[0] = 0;
} else {
pbuf2 = (char *)malloc(strlen(pbuf1) + strlen(str) + strlen(str2) + 1);
strcpy(pbuf2, pbuf1);
free(pbuf1);
pbuf1 = pbuf2;
}
strcat(pbuf1, str);
strcat(pbuf1, str2);
*pstr1 = ch;
str = pstr1 + strlen(str1);
pstr1 = strstr(str, str1);
}
if(str&&str[0]) {
if(pbuf1 == NULL) {
pbuf1 = (char *)malloc(strlen(str) + strlen(str2) + 1);
pbuf1[0] = 0;
} else {
pbuf2 = (char *)malloc(strlen(pbuf1) + strlen(str) + strlen(str2) + 1);
strcpy(pbuf2, pbuf1);
free(pbuf1);
pbuf1 = pbuf2;
}
strcat(pbuf1, str);
}
return pbuf1;
}
int main(int argc, char ** argv) {
char * pstr;
if(argc != 4) {
fprintf(stderr, "Error, parameter\n");
return 1;
}
pstr = replace(argv[1], argv[2], argv[3]);
fprintf(stdout, "replace(\"%s\", \"%s\", \"%s\") = \"%s\"\n",
argv[1], argv[2], argv[3], pstr);
free(pstr);
return 0;
}
阅读(1597) | 评论(0) | 转发(0) |