In this C++ tutorial, you will learn how to remove all occurrences of a specific character from a given string using different techniques, with example programs.
Remove all occurrences of specific character from string in C++
To remove all the occurrences of a specified character from a given input string in C++, you can use a standard For loop to iterate over the characters and exclude the character if there is a match, or you can use remove() function of algorithm library.
1. Remove all occurrences of a character from the string using For loop in C++
In the following program, we take a string in str
and a character in charToRemove
. We have to remove all the occurrence of the given character from the string.
Steps
- Given an input string in
str
, character to be removed from the string incharToRemove
. - Take a string
result
to store the resulting string. - Use a C++ For loop to iterate over the characters of the input string
str
.- Inside the For loop, if the character is not equal to
charToRemove
, then append the character to the resulting stringresult
.
- Inside the For loop, if the character is not equal to
- After the completion of For loop, we get a string in
result
where the value is equal to the input string with all the occurrences of specified character removed.
Program
main.cpp
#include <iostream>
#include <string>
using namespace std;
int main() {
string str = "Hello World";
char charToRemove = 'o'; // Character to remove
string result = "";
for (char c : str) {
if (c != charToRemove) {
result += c;
}
}
cout << result << endl;
return 0;
}
Output
Hell Wrld
This approach of using For loop does not modify the original string.
2. Remove all occurrences of a character from the string using remove() function of algorithm library in C++
In the following program, we take a string in str
and a character in charToRemove
. We have to remove all the occurrence of the given character from the string.
Steps
- Given an input string in
str
, character to be removed from the string incharToRemove
. - Use the
remove
algorithm to remove all occurrences ofcharToRemove
from the stringstr
. Theremove
algorithm rearranges the elements in the range[str.begin(), str.end())
so that all elements that are equal tocharToRemove
are moved to the end of the range. It returns an iterator pointing to the new end of the range, where the removed elements begin. - Use the
erase()
method of the string to remove the characters from the iterator returned byremove()
to the end of the string. This effectively erases all the occurrences of specified character from the string. - Print the modified given string str.
Program
main.cpp
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main() {
string str = "Hello World";
char charToRemove = 'o'; // Character to remove
str.erase(std::remove(str.begin(), str.end(), charToRemove), str.end());
cout << str << endl;
return 0;
}
Output
Hell Wrld
This approach of using remove
algorithm and string erase()
method modifies the original string.
Conclusion
In this C++ Tutorial, we learned how to remove all the occurrences of a specific character from given input string, with examples.