remove all occurrences of a given character from a string.

leighton > I'm leighton and I would like your help with programming
leighton > as im struggling to create a program which removes a certain character from a string.

The canonical way to do this is to use the remove-erase idiom.
http://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Erase-Remove

1
2
3
4
5
6
7
8
9
#include <algorithm>
#include <string>

std::string remove_char( std::string str, char ch )
{
    // remove all occurrences of char ch from str
    str.erase( std::remove( str.begin(), str.end(), ch ), str.end() ) ;
    return str ;
}

thank you, really means a lot appreciate it and it works well....
Topic archived. No new replies allowed.