C++ 删除字符串中的指定子字符串

来源:互联网 发布:中国软件排名 编辑:程序博客网 时间:2024/05/19 16:51

This question has been really interesting since you not only find the specific char, and you should connect the char after it to the one in front of it.


The c ++ code is written as follow:
void del_char(char str[], char c)
{
    int i = 0, j = 0;
    while(str[i] != '\0')
    {
        if(str[i] != c)
        {
            str[j++] = str[i++];
        }
        else
        {
            i++;
        }
    }
    str[j] = '\0';
}


C/C++_功能实现_删除字符串中的指定子字符串

char *del_substr(char *str, char *delstr)
{
    char *p, *q;
    char *src, *dst;
    dst = src = str;
    while(*src != '\0')
    {
        p = src;
        q = delstr;
        while(*p == *q && *q != '\0')
        {
           p++;
           q++;
        }
        if (*q == '\0')
        {
           src = p;
        }
        else
        {
           *dst++ = *src++;
        }
    }
    *dst = '\0';
    return str;
}

0 0