中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

C語言編寫的ReplaceAll函數(shù)

2018-07-20    來源:open-open

容器云強勢上線!快速搭建集群,上萬Linux鏡像隨意使用
#include <stdio.h>
#include <malloc.h>
#include <string.h>
 
char* replaceAll(char* src, char* find, char* replaceWith){
    //如果find或者replace為null,則返回和src一樣的字符串。
    if(find == NULL || replaceWith == NULL){
        return strdup(src);
    }
    //指向替換后的字符串的head。
    char* afterReplaceHead = NULL;
    //總是指向新字符串的結(jié)尾位置。
    char* afterReplaceIndex = NULL;
    //find字符串在src字符串中出現(xiàn)的次數(shù)
    int count = 0;
    int i,j,k;
     
    int srcLen = strlen(src);
    int findLen = strlen(find);
    int replaceWithLen = strlen(replaceWith);
     
    //指向src字符串的某個位置,從該位置開始復制子字符串到afterReplaceIndex,初始從src的head開始復制。
    char* srcIndex = src;
    //src字符串的某個下標,從該下標開始復制字符串到afterReplaceIndex,初始為src的第一個字符。
    int cpStrStart = 0;
    
    //獲取find字符串在src字符串中出現(xiàn)的次數(shù)
    count = getFindStrCount(src, find);
    //如果沒有出現(xiàn),則返回和src一樣的字符串。
    if(count == 0){
        return strdup(src);
    }
     
    //為新字符串申請內(nèi)存
    afterReplaceHead = afterReplaceIndex = (char*)malloc(srcLen + 1 + (replaceWithLen - findLen) * count);
    //初始化新字符串內(nèi)存
    memset(afterReplaceHead, '\0',sizeof(afterReplaceHead));
 
    for(i = 0,j = 0,k = 0;i!=srcLen;i++){
        //如果find字符串的字符和src中字符串的字符是否相同。
        if(src[i] == find[j]){
            //如果剛開始比較,則將i的值先賦給k保存。
            if(j == 0){
                k = i;
            }
            //如果find字符串包含在src字符串中
            if(j == (findLen-1)){
                j = 0;
                //拷貝src中find字符串之前的字符串到新字符串中
                strncpy(afterReplaceIndex, srcIndex, i - findLen - cpStrStart + 1);
                //修改afterReplaceIndex
                afterReplaceIndex = afterReplaceIndex + i - findLen - cpStrStart + 1;
                //修改srcIndex
                srcIndex = srcIndex + i - findLen - cpStrStart + 1;
                //cpStrStart
                cpStrStart = i + 1;            
 
                //拷貝replaceWith字符串到新字符串中
                strncpy(afterReplaceIndex, replaceWith, replaceWithLen);
                //修改afterReplaceIndex
                afterReplaceIndex = afterReplaceIndex + replaceWithLen;
                //修改srcIndex
                srcIndex = srcIndex + findLen;
            }else{
                j++;
            }
        }else{
            //如果find和src比較過程中出現(xiàn)不相等的情況,則將保存的k值還給i
            if(j != 0){
                i = k;
            }
            j = 0;
        }
    }
    //最后將src中最后一個與find匹配的字符串后面的字符串復制到新字符串中。
    strncpy(afterReplaceIndex, srcIndex, i - cpStrStart);
     
    return afterReplaceHead;
}
 
int getFindStrCount(char* src, char* find){
    int count = 0;
    char* position =src;
    int findLen = strlen(find);
    while((position = strstr(position, find)) != NULL){
        count++;
        position = position + findLen;
    }
    return count;
}
 
int main(void){
    char* s = "12345345443344341334542345";
    //調(diào)用函數(shù)replaceAll,s為源字符串,"345"為需要替換的字符串,”7890“為替換目的字符串。
    char* r = replaceAll(s, "345", "7890");
    printf("%s\n",r);
    //使用replaceAll函數(shù)后一定要free掉,因為replace內(nèi)部會malloc結(jié)果字符串的內(nèi)存。
    free(r);
    return 0;
}

標簽:

版權(quán)申明:本站文章部分自網(wǎng)絡,如有侵權(quán),請聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請與原作者聯(lián)系。

上一篇:C++時間復雜度為O(n)隨機生成不重復的數(shù)代碼

下一篇: c/c++ 中文字符串轉(zhuǎn)Unicode和UTF8