Замена одной строки другой в C - PullRequest
0 голосов
/ 18 сентября 2011

У меня есть строка

{"status":true}

Я хочу заменить &quot на ".Я пробовал несколько строковых операций, но они не работают.

1 Ответ

1 голос
/ 08 октября 2011
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char *replace (char *this, char *withthat, char *inthis) {
    char *where = inthis;
    if(strlen(withthat)>strlen(this)) {
        fprintf(stderr, "replace can only shrink\n");
        exit(EXIT_FAILURE);
    }
    while ((where = strstr(where, this))) {
        memcpy(where, withthat, strlen(withthat));
        memmove(where+strlen(withthat),where+strlen(this), strlen(where+strlen(this))+1);
    }
    return inthis;
}

int main(void) {
    char string[] = "{&quot;status&quot;:true}";
    printf("%s\n", replace("&quot;", "\"", string));
    return 0;
}

Выход:

{"status":true}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...