In this article, we are going to see how to remove character from the string in C++.
Remove a character from string without using library function in C++
We can remove a single character without using any library function. Following is the implementation. The character to be removed can have multiple occurrences.
#includeusing namespace std; int main(){ //declare string string str; //input str cout<<"Input string:\n"; cin>>str; cout<<"Enter the character you want to remove\n"; char c; cin>>c; //initialize a new string which will not contain the character to be removed string newstr=""; for(int i=0;i Output:
Input string:
java2blog
Enter the character you want to remove
a
Converted string is: jv2blogRemove multiple characters from string without using library function in C++
We can remove multiple characters without using any library function. Following is the implementation. Each character to be removed can have multiple occurrences.
#includeusing namespace std; int main(){ //declare string string str; //input str cout<<"Input string:\n"; cin>>str; cout<<"Enter the characters you want to remove as a string\n"; string rmv; cin>>rmv; //initialize a new string which will notcontain the character to be removed string newstr=""; for(int j=0;j Output:
Input string:
java2blog.com
Enter the characters you want to remove as a string
ao
Converted string is: jv2blg.cmstd::remove() and string::erase() to remove character from string in C++
remove() is a
library function which removes certain characters from the string. The usage can be seen in the following implementation. Actually it’s a function which removes certain value from a range. So the string is basically created into a range using iterator .begin()and.end(). The remove function returns the new end of the range, an iterator pointing to the last element not removed.
Erase the remaining part of the string, i.e., if it be the iterator returned from remove function then erase it to string.end()#includeusing namespace std; int main(){ //declare string string str; //input str cout<<"Input string:\n"; cin>>str; cout<<"Enter the characters you want to remove as a string\n"; string rmv; cin>>rmv; string::iterator it; for(int i=0;i Output:
Input string:
java2blog.com
Enter the characters you want to remove as a string
ao
Converted string is: jv2blg.cmstd::remove_if() and string::erase() to remove character from string in C++
In the above solution, remove function is called for every character to be removed in the for loop. We can avoid that by using remove_if() function. It’s also a standard library function. In remove_if() we have an predicate function involved which returns true for characters to be removed. Implementation is following:
#includeusing namespace std; string rmv; bool myfunc(char c){ if(rmv.find(c)!=string::npos) return true; return false; } int main(){ //declare string string str; //input str cout<<"Input string:\n"; cin>>str; cout<<"Enter the characters you want to remove as a string\n"; cin>>rmv; string::iterator it; it=remove_if(str.begin(),str.end(),myfunc); //myfunc is predicate function,rmv is removed from str str.erase(it,str.end()); //erase this part cout<<"Converted string is: "< Output:
Input string:
java2blog.com
Enter the characters you want to remove as a string
ao
Converted string is: jv2blg.cmObviously, this code is not straight forward as it was for remove function(). Here we haven’t used the for loop, we have lot iterated through all character to be removed, then how it is working?
remove_if() function has an argumentpredicatefunction. Apredicatefunction is a Boolean function which is running for every elements in the range (str.begin(), str.end())
Our predicate is namely myfunc which isbool myfunc(char c){ if(rmv.find(c)!=string::npos) return true; return false; }
rmvis declared as a global variable to be noted, so that in can be used in the predicate function. Now for every elementc, we have checked whether it is part of the string which contains character to be removed or not. To do this, we have usedstring.find()function which returnsstring::nposif character not found. Based on this predicate function, the characters has been removed or unchanged.That's all about how to remove character from string in C++.