实现字符串中子字符串的替换(待替换字符串和替换字符串的长度可以不相等)

//使用C语言实现字符串中子字符串的替换
//描述:编写一个字符串替换函数,如函数名为 StrReplace(char* strSrc, char* strFind, char* strReplace),
//strSrc为原字符串,strFind是待替换的字符串,strReplace为替换字符串。
//举个直观的例子吧,如:“ABCDEFGHIJKLMNOPQRSTUVWXYZ”这个字符串,把其中的“RST”替换为“ggg”这个字符串,
//结果就变成了:ABCDEFGHIJKLMNOPQgggUVWXYZ

#include<stdio.h>
#include<string.h>

void StrReplace(char* strSrc, char* strFind, char* strReplace)
{
    int i,j,k,m;
    int lengthSrc,lengthFind,lengthReplace;
    lengthSrc = strlen(strSrc);
    lengthFind = strlen(strFind);
    lengthReplace = strlen(strReplace);

    for(i=0;i<lengthSrc;)
    {
        j = 0;
        if(strSrc[i] == strFind[j])//遍历原字符串,如果当前字符与strFind[0]相等,开始逐位判断
        {
            do
            {
                i++;j++;
            }while((j < lengthFind) && (strSrc[i] == strFind[j]));

            //判断跳出while循环的条件。如果j == lengthFind表示找到了匹配的字符串。且i指向strSrc中匹配字符串strFind的下一个字符。
            if(j == lengthFind)
            {
                //strFind字符串和strReplace字符串的长度相等时
                if(lengthFind == lengthReplace)
                {
                    for(k=i-lengthFind,m=0;m<lengthReplace;k++,m++)
                    {
                        strSrc[k] = strReplace[m];
                    }
                }

                //strFind字符串的长度小于strReplace字符串的长度
                if(lengthFind < lengthReplace)
                {
                    //增加空位。
                    for(k=lengthSrc;k>=i;k--)
                    {
                        strSrc[k+lengthReplace-lengthFind] = strSrc[k];
                    }

                    //strSrc长度更新。如果不更新,在下一次增加空位时,会有问题。
                    lengthSrc += lengthReplace-lengthFind;

                    //开始替换。
                    for(k=i-lengthFind,m=0;m<lengthReplace;k++,m++)
                    {
                        strSrc[k] = strReplace[m];
                    }

                    //strSrc的元素和长度变更后,需要将i重新指向已替换部分的下一个字符。
                    i+= lengthFind-lengthReplace;
                }
                
                //strFind字符串的长度大于strReplace字符串的长度
                if(lengthFind > lengthReplace)
                {
                    //减小空位。
                    for(k=i;k<=lengthSrc;k++)
                    {
                        strSrc[k-(lengthFind-lengthReplace)] = strSrc[k];
                    }

                    //strSrc长度更新。如果不更新,在下一次减小空位时,会有问题。
                    lengthSrc -= lengthFind-lengthReplace;

                    //开始替换。
                    for(k=i-lengthFind,m=0;m<lengthReplace;k++,m++)
                    {
                        strSrc[k] = strReplace[m];
                    }

                    //strSrc的元素和长度变更后,需要将i重新指向已替换部分的下一个字符。
                    i-= lengthFind-lengthReplace;
                }
            }
        }
        else
        {
            i++;
        }
    }
}

int main()
{
    char strSrc[255],strFind[255],strReplace[255];
    gets(strSrc);
    gets(strFind);
    gets(strReplace);
    
    StrReplace(strSrc,strFind,strReplace);
    puts(strSrc);
    return 0;
}
原文地址:https://www.cnblogs.com/Camilo/p/3837806.html