Search code examples
clinuxstringmultibytewidechar

Remove special char from string in C under Linux


Using C in Linux, how can I remove character (or any other "specific" non ASCII character - passed as a parameter to the function -) from a string?

I have tried with:

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

but I get:

:warning: multi-character character constant.

Should I convert before the string in wide char something like

wchar_t wsAux[100];
remove_all_chars(wsAux, "A€bcd", 100);

but it doesn't work.


Solution

  • I've tried:

    void removeSubstring(char *s,const char *toremove)
    {
      while( s=strstr(s,toremove) )
       memmove(s,s+strlen(toremove),1+strlen(s+strlen(toremove)));
    }
    

    from Removing substring from a string? and it works.

    In this way then € symbol is treated as string, in fact it occupy 3 bytes ( try strlen("€") )!