Delete char from a char array.

Hello everybody,
I want to ask how is it possible to delete a specific char from a char array?
I loop through the array with a for, I find the character that I want to erase, and then what?

Should I move all the elements of the map one position to the left?

And then you need to copy the tail of the string into the position of the erased character.:)
For example if you may use C standard functions then the code could look the following way

char *p = strchr( s, c );

if ( p ) strcpy( p, p + 1 );

But I think your task is to remove all occurances of a given character. In this case instead of using strcpy you should write your own loop that during the coping to see whether there is one more character that shall be removed.

In C++ there is standard algorithm std::remove. So you can write simply

std::remove( s, s + std::strlen( s ), c );

Or even

*std::remove( s, s + std::strlen( s ), c ) = '\0';
Last edited on
Thank you : )
Topic archived. No new replies allowed.