Remove all '\' from a string in C

3476 views c

I am trying to get rid of all \ characters in a string in C. For example, if a string is co\din\g it should convert the string to coding.

So far I have the code

  for(i = 0; i < strlen(string); ++i){
    if(string[i] == '\'){


That looks to see if there is a backslash. I don't know how I would do to remove the backslash, however. My only idea is to set the next character equal to the current character, however, I don't know how changing the length of the string would work with memory.

answered question

1 Answer


The simplest solution would be to use a second string for the result:

char result[100] = {'\0'};

for(int i = 0, j = 0; i < strlen(string); i++)
    if(string[i] != '\')
        result[j++] = string[i];

posted this

Have an answer?


Please login first before posting an answer.