标签:blog class code ext c color
自己写的一段:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | //goolchar* str_replace(char* source, constchar* find, constchar* replace){    if(source == NULL || find == NULL || find == "")        returnstrdup(source);        intmatchCount = 0;    intnowIndex = 0;    intfindLength = strlen(find);    intreplaceLength = strlen(replace);    intsourceLength = strlen(source);    intresultLength = sourceLength;    char* result;    inti;    for( i = 0; i < sourceLength; i++)    {        if(nowIndex < findLength && source[i] == find[nowIndex]){            nowIndex++;            matchCount++;        }        else        {            if(matchCount == findLength)            {                source[i - 1] = ‘\0‘;                nowIndex = 0;                matchCount = 0;                resultLength -= findLength - replaceLength;            }        }    }    nowIndex = 0;    matchCount = 0;    result = (char*)malloc(2 * (resultLength + 1));    for( i = 0; i < sourceLength; i++)    {        if(source[i] == ‘\0‘){            source[i] = find[0];            while(matchCount < replaceLength)            {                *(result + nowIndex + matchCount) = replace[matchCount];                matchCount++;            }            matchCount = 0;            nowIndex += replaceLength;            i += findLength -1;        }        else        {            *(result + nowIndex) = source[i];            nowIndex++;        }    }    *(result + nowIndex + 1) = ‘\0‘;    returnresult;} | 
http://blog.csdn.net/glmmmm/article/details/9930969:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | char*str_replace_2(constchar*string, constchar*substr, constchar*replacement){    char*tok = NULL;    char*newstr = NULL;    char*oldstr = NULL;    /* if either substr or replacement is NULL, duplicate string a let caller handle it */    if(substr == NULL || replacement == NULL)        returnstrdup(string);    newstr = strdup(string);    while((tok = strstr(newstr, substr)))    {        oldstr = newstr;        newstr = (char*)malloc(strlen(oldstr) - strlen(substr) + strlen(replacement) + 1);        /*failed to alloc mem, free old string and return NULL */        if(newstr == NULL)        {            free(oldstr);            returnNULL;        }        memcpy(newstr, oldstr, tok - oldstr);        memcpy(newstr + (tok - oldstr), replacement, strlen(replacement));        memcpy(newstr + (tok - oldstr) + strlen(replacement), tok + strlen(substr), strlen(oldstr) - strlen(substr) - (tok - oldstr));        memset(newstr + strlen(oldstr) - strlen(substr) + strlen(replacement), 0, 1);        free(oldstr);    }    returnnewstr;} | 
http://stackoverflow.com/questions/779875/what-is-the-function-to-replace-string-in-c:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | char*str_replace_3(char*orig, char*rep, char*with) {    char*result; // the return string    char*ins;    // the next insert point    char*tmp;    // varies    intlen_rep;  // length of rep    intlen_with; // length of with    intlen_front; // distance between rep and end of last rep    intcount;    // number of replacements    if(!orig)        returnNULL;    if(!rep)        rep = "";    len_rep = strlen(rep);    if(!with)        with = "";    len_with = strlen(with);    ins = orig;    for(count = 0; tmp = strstr(ins, rep); ++count) {        ins = tmp + len_rep;    }    // first time through the loop, all the variable are set correctly    // from here on,    //    tmp points to the end of the result string    //    ins points to the next occurrence of rep in orig    //    orig points to the remainder of orig after "end of rep"    tmp = result = (char*)malloc(strlen(orig) + (len_with - len_rep) * count + 1);    if(!result)        returnNULL;    while(count--) {        ins = strstr(orig, rep);        len_front = ins - orig;        tmp = strncpy(tmp, orig, len_front) + len_front;        tmp = strcpy(tmp, with) + len_with;        orig += len_front + len_rep; // move to next "end of rep"    }    strcpy(tmp, orig);    returnresult;} | 
平均测速:1. 84 clock/100000,2. 195 clock/100000,3.89 clock/100000
clang 简单的str_replace实现,布布扣,bubuko.com
标签:blog class code ext c color
原文地址:http://www.cnblogs.com/Gool/p/3721322.html