C语言小函数——删除字符串str1中含有的字符串str2

来源:互联网 发布:王尼玛有没有开淘宝店 编辑:程序博客网 时间:2024/06/05 05:23
本函数实现的是删除str1中的含有的所有str2 。
char *delstr(char *src, const char *sub){    char *st = src, *s1 = NULL;    const char *s2 = NULL;    while (*st&& *sub)    {        s1 = st;        s2 = sub;        while (*s1 && *s2 &&!(*s1 - *s2))        {            s1++;            s2++;        }        if (!*s2)        {            while (*st++=*s1++);            st = src;          }        st++;    }    return (src);}int main(){    char s0[20] = "abcdefg";     char *s1 = "bc";    printf("result:%s\r\n", delstr(s0, s1));    system("pause");    return 0;}

运行结果:这里写图片描述

阅读全文
0 0
原创粉丝点击