char *strrpl(char *s, const char *s1, const char *s2)
{
char *ptr;
char *cur_p = s;
while ((ptr = strstr(cur_p, s1)) != NULL) /* 如果在s中找到s1 */
{
memmove(ptr + strlen(s2) , ptr + strlen(s1), strlen(ptr) - strlen(s1) + 1);
memcpy(ptr, &s2[0], strlen(s2));
cur_p = ptr + strlen(s2); //避免死循环,只替换一次
}
return s;
}
int main(void)
{
char buf[2048] = "{\"qrType\"}";
printf("%s\n", buf);
strrpl(buf, "\"", "\\\"");
printf("%s\n", buf);
}