All pastes #508172 Raw Edit

crude str_replace

public c v1 · immutable
·published 2007-05-24 21:57 UTC
rendered paste body
static char *str_replace(const char * s1, const char * s2, char * string){    char * res = NULL;    size_t nb_entry = 0;    size_t diff_size = strlen(s2) - strlen(s1);    size_t size_res;    const char * p = string;    char * p_res;    const char * t;    while ((t = strstr(p,s1)) != NULL) {        nb_entry ++;        p = t + 1;    }// OK nous connaissons le nombre d'entree a remplacer    size_res = strlen(string) + 1 + nb_entry * diff_size;    res = malloc(size_res);    p = string;    p_res = res;    strcpy(res,"");    while ((t = strstr(p,s1)) != NULL) {        strncat(p_res,p,t-p);        strcat(p_res,s2);        p_res += (t-p);        p_res += strlen(s2);        p = t + strlen(s1);    }    sprintf(p_res,"%s",p);    return res;}