Home > Software engineering >  C deleting char in string
C deleting char in string

Time:03-29

Now I'm using this code to delete some char in a string.

void eliminate(char *str, char ch){
    for(; *str != '\0';str  ){
        if(*str == ch){
            strcpy(str, str 1);
            str--;
        }
    }
}

In the char *str there are some strings like

"sll $6, $5, 16"

After deleting "$", the string looks like this.

"sll 6, 5, 16"

But after deleting ",", the string became very strange.

"sll 6 5 6"

Is there any problem with the code above? And also, it only happens in Linux and online GDB. VS code in my window laptop eliminates the targeted char very well.

CodePudding user response:

As pointed out in comments strcpy() is not safe when coping data with overlapping memory blocks. memmove(dst, src, len) is the alternative which uses an auxiliary buffer in case of src & dst memory overlaps.

You can simply skip the character to eliminate in a loop:

#include <stdio.h>

void drop_char (char *str, char ch) {
    if (!str) return;

    for(char* cp = str; 1 ; ) {
        if(*cp != ch)
            *str   = *cp;
        if ('\0' == *cp  )
            break;
    }
}

int main () {
    char str [] = "sll     $6, $5, 16";
    printf ("Original   : [%s]", str);

    drop_char(str, '$');
    printf ("\nDropping $ : [%s]", str);

    drop_char(str, ',');
    printf ("\nDropping , : [%s]", str);

    printf ("\n");

    return 0;
}

CodePudding user response:

Without using explicit pointer math, we can use two indices as we iterate over the input string. i is an index with increments on each iteration, and j which only increments when the target character is not found.

void drop_char(char *str, char ch) {
    size_t i = 0, j = 0;    

    for (; str[i];   i) {
        if (str[i] != ch) 
            str[j  ] = str[i];
    }

    str[j] = '\0';
}

If we have the string char test[] = "hello" and call drop_char(test, 'l') the process looks like:

i = 0, j = 0
char: 'h'
'h' != 'l'
test[0] = 'h'

i = 1, j = 1
char: 'e'
'e' != 'l'
test[1] = 'e'

i = 2, j = 2
char: 'l'
'l' == 'l'

i = 3, j = 2
char: 'l'
'l' == 'l'

i = 4, j = 2
char: 'o'
'o' != 'l'
test[2] = 'o'

i = 5, j = 3
test[3] = '\0'
  • Related