Remove Character from String in C++

Remove character from String in C++

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.

Output:

Input string:
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.

Output:

Input string:
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 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()

Output:

Input string:
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:

Output:

Input string:
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

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++.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *