Table of Contents
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
#include <bits/stdc++.h> using 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<str.length();i++){ if(str[i]!=c){ //if character not to be removed // string(1,x) converts character 'x' to string "x" newstr+=string(1,str[i]); //apend to new str } } cout<<"Converted string is: "<<newstr; } |
Output:
java2blog
Enter the character you want to remove
a
Converted string is: jv2blog
Remove 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
#include <bits/stdc++.h> using 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<str.length();j++){ int flag=0; for(int i=0;i<rmv.length();i++){ //for each character to remove if(str[j]==rmv[i]){ //if character is to be removed flag=1; break; } } if(flag==0){ //if character not to be removed then append // string(1,x) converts character 'x' to string "x" newstr+=string(1,str[j]); //apend to new str } } cout<<"Converted string is: "<<newstr; } |
Output:
java2blog.com
Enter the characters you want to remove as a string
ao
Converted string is: jv2blg.cm
std::remove() and string::erase() to remove character from string in C++
remove() is a .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()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
#include <bits/stdc++.h> using 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<rmv.length();i++){ //for each character to remove it=remove(str.begin(),str.end(),rmv[i]); //rmv[i] is removed str.erase(it,str.end()); //erase this part } cout<<"Converted string is: "<<str; } |
Output:
java2blog.com
Enter the characters you want to remove as a string
ao
Converted string is: jv2blg.cm
std::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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
#include <bits/stdc++.h> using 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: "<<str; } |
Output:
java2blog.com
Enter the characters you want to remove as a string
ao
Converted string is: jv2blg.cm
Obviously, 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 argument predicate
function. A predicate
function is a Boolean function which is running for every elements in the range (str.begin(), str.end())
Our predicate is namely myfunc which is
1 2 3 4 5 6 7 8 |
bool myfunc(char c){ if(rmv.find(c)!=string::npos) return true; return false; } |
rmv
is declared as a global variable to be noted, so that in can be used in the predicate function. Now for every element c
, we have checked whether it is part of the string which contains character to be removed or not. To do this, we have used string.find()
function which returns string::npos
if 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++.