How to remove all occurrences of a given character from string in C?

后端 未结 7 604
有刺的猬
有刺的猬 2020-11-30 11:24

I\'m attempting to remove a character from a string in C. The problem I am having with my code is that it removes the first instance of the character from the string but als

7条回答
  •  误落风尘
    2020-11-30 11:56

    char str1[30] = "Hello", *prt1, c = 'l';
    char str2[30], *prt2;
    prt1 = str1;
    prt2 = str2;
    while(*prt1 != 0)
    {
        if(*prt1 != c)
        {
             *prt2 = *prt1;
             prt2++;  
        }
        prt1++;
    }
    *prt2 = '\0';
    

提交回复
热议问题