Replace

void Replace(char* str, const char* toFind, const char* toReplace)
{
int s = strlen(str);
int f = strlen(toFind);
int r = strlen(toReplace);
int counter = 0;

while (char* found = strstr(str, toFind))
{
    counter++;
    if (f < r)
    {
        int size = (s + 1) - (found + f - str);
        if (size > 0)
        {
            memmove(found + r, found + f, (s + 1) - (found + f - str));
        }
        strncpy(found, toReplace, r);
    }
    else
    {
        strncpy(found, toReplace, r);
        int size = (s + 1) - (found + f - str);
        if (size > 0)
        {
            memmove(found + r, found + f, (s + 1) - (found + f - str));
        }
    }
}

str[s + counter*(r - f)] = '\0';

}

猜你喜欢

转载自blog.csdn.net/weixin_41256413/article/details/82285192