简体   繁体   中英

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

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 also wipes everything after that character in the string too. For example, removing 'l' from 'hello' prints 'he' rather than 'heo'

int i;
char str1[30] = "Hello", *ptr1, c = 'l';
ptr1 = str1;
for (i=0; i<strlen(str1); i++)
{
    if (*ptr1 == c) *ptr1 = 0;
    printf("%c\n", *ptr1);
    ptr1++;
}

I need to use pointers for this and would like to keep it as simple as possible since I'm a beginner in C. Thanks

You can do it like this:

void remove_all_chars(char* str, char c) {
    char *pr = str, *pw = str;
    while (*pr) {
        *pw = *pr++;
        pw += (*pw != c);
    }
    *pw = '\0';
}

int main() {
    char str[] = "llHello, world!ll";
    remove_all_chars(str, 'l');
    printf("'%s'\n", str);
    return 0;
}

The idea is to keep a separate read and write pointers ( pr for reading and pw for writing), always advance the reading pointer, and advance the writing pointer only when it's not pointing to a given character.

If you remove the characters in place you will have to shift the rest of the string one place to the left every time you remove a character, this is not very efficient. The best way is to have a second array that takes the filtered string. For example you can change your code like this.

int i;
char str1[30] = "Hello", *ptr1, c = 'l';
char str2[30] = {0}, *ptr2;
ptr1 = str1;
ptr2 = str2;
for (i=0; i<strlen(str1); i++)
{
    if (*ptr1 != c) *ptr2++=*ptr1;
    ptr1++;
}
printf("%s\n", str2);
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';

i know that it is a type of duplicate answer, but this code is function's version for solving the problem. I thought that as the questioner was a beginner, he might learn much from decomposed version of problem.

int del_x_char(char *p, int x)
{
    char *q;
    x=first_occurance(p, 'i')/*you can replace any character that you want delete with 'i'*/
    q=p+x;
    while(*q=*(q+1))
        q++;
    *q='\0';
    return 0;
}
int first_occurance(char *q, char phar)
{
    int i=0;
    while(*q)
    {   
        if(*q++==phar)
            return i;
        i++;
    }
    return -1;
}

The problem is that when you encounter the first character that matches c , you insert a null character right there. That means you're essentially cutting off the rest of the string.

What you need to do is when you find a matching character, move the following characters back one position. Then you need to insert the null character at the very end depending on how many characters you have removed.

just change

if (*ptr1 == c) *ptr1 = 0;

to

if (*ptr1 == c) continue;

as @ouah said, it breaks at the first NULL character..

C将字符串定义为“由第一个空字符终止并包括第一个空字符的连续字符序列”

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM