Skip to content

Instantly share code, notes, and snippets.

@ravichandrae
Created October 11, 2013 13:49
Show Gist options
  • Save ravichandrae/6934964 to your computer and use it in GitHub Desktop.
Save ravichandrae/6934964 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <string>
using namespace std;
void removeChars(string &str, char ch)
{
int i;
int resInd = 0; //Result string index
for( i = 0; i < str.length(); i++ )
{
//retain str[i] only if it is not ch
if( str[i] != ch )
{
str[resInd] = str[i];
resInd++;
}
}
//Resize the string so that chars beyond resInd will be erased.
str.resize(resInd);
}
int main()
{
string str;
getline(cin, str);
char removeCh;
cin >> noskipws >>removeCh;
cin >>skipws;
removeChars(str, removeCh);
cout<<str;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment