There are a few methods for removing punctuation symbols from a string in C++. In this article, we’ll go through how to remove punctuation marks from a string using different methods in C++.
Example 1: Program of C-Style Method to Remove Punctuation in C++
This implementation eliminates all symbols except alphabets from a string by using the C-style string method entered by the user.
First, we have the main method of the program. In the main, we have a char array as “StrLine” and set the limit of the char array to “50”. Also, we have defined another char array, “CharString” of limit “50”. Then, an “int” variable as “n” and initialize it with the zero. The cout command will print the statement “Input a string” for a user. Through the cin.getline approach, the user will enter the string. The string entered by the user will be checked in the for loop condition, which will verify whether the “StrLine” character is not at the end of the string.
Then the, if statement will execute the condition that the string entered has the lowercase and uppercase alphabet and check whether the string has just characters or not. We have initialized the “CharString” array with the zero characters upon which the program terminated as the string has its end value of zero characters.
using namespace std;
int main() {
char StrLine[50], CharString[50];
int n = 0;
cout<= 'a' &&StrLine[i]= 'A' &&StrLine[i]<='Z'))
{
CharString[n++] = StrLine[i];
}
}
CharString[n] = '\0';
cout<< "Resultant String: " <<CharString<<"\n";
return 0;
}
The string entered by the user has some punctuation characters, which are removed in the resultant string by the C-style method.
Example 2: Program of Using std::remove_if Method to Remove Punctuation in C++
A simple option to remove punctuation from a string is to utilize the standard algorithm “std::remove_if” with the string::erase member function. As the algorithm “std::remove_if” does not have access to the string container, it can only remove the punctuation marks in the string. It outputs an iterator indicating where the termination should be, which can be removed using the std::erase method.
The main method of this program has a standard string class way of declaring a string as “MyString” and initializing it with the string containing some punctuation character. Then, we have an “auto” type variable as “remove,” We used the method std::remove_if. In the method, we have a begin() function for the first characters iterator of the string and the end() for the last characters iterator for the string “MyString”. We have an array char const of reference variable “s”. It creates a copy of each character.
The ispunct() is then called to check the array containing elements is character. After that erase method is used which removes the punctuation from the string up to the last character of the string.
#include <string>
#include <algorithm>
#include <cctype>
int main()
{
std::string MyString = "He!@||ll@o.++|";
auto remove = std::remove_if(MyString.begin(), MyString.end(), []
(char const &s)
{
return std::ispunct(s);
});
MyString.erase(remove, MyString.end());
std::cout<<MyString<< std::endl;
return 0;
}
The string shown in the image has no punctuation symbol; just the string with the characters is returned.
Example 3: Program of Using a Reverse Loop to Remove Punctuation in C++
Alternatively, we may use a conventional for loop to locate punctuations in the given string and erase them with the string::erase method. The loop should be in reverse order to avoid non-deterministic performance while removing components during iteration.
The string is defined with the name “String_str” as in a standard class of string, and the string contains the string with some alphabet characters and some punctuation symbols. After the string declaration, we have a for loop which will iterate over each string character in reversed order. Then, we have an ispunct function that verifies punctuation characters in the specified string in the if condition. If any punctuation character is found, it will be erased in the erase function.
#include <string>
#include <cctype>
int main()
{
std::string String_str = "C`|plus[plusPro@gr@@^a&mm!-ing";
for (int i = String_str.size() - 1; i>= 0; i--) {
if (ispunct(String_str[i])) {
String_str.erase(i, 1);
}
}
std::cout<< "String :" <<String_str<< std::endl;
return 0;
}
The resultant string has no punctuation character, as shown in the following image.
Example 4: Program Using a Custom Function to Remove Punctuation in C++
Alternatively, the previous procedure can be moved to a separate function, which generates a local copy of the string and operates on it, returning the formatted value to the original code. The Custom function is used to enhance the functionality to enable distinct character sets or even to pass the custom criterion function for the remove_if algorithm’s third parameter.
We have the function definition as “RemovePunctuation” in the following program and passed the string reference “str” as a parameter. In the function, we have “temp” as a function to which we pass “str. Then, we have an erase function calling the std::remove_if function in it.
After that, we have the main method where we have defined and initialized a string “content”. Also, the function invoked the above-specified function “RemovePunctuation” here to which the string “content” is passed. The parsed string will be printed after removing the punctuation symbols.
#include <string>
#include <algorithm>
#include <cctype>
using std::cout; using std::cin;
using std::endl; using std::string;
string RemovePunctuation(const string& str) {
string temp(str);
temp.erase(std::remove_if(temp.begin(), temp.end(), ispunct), temp.end());
return temp;
}
int main(){
string content = "Have| ,! a@@ ^() g^^o!od d@++ay|?";
cout<< "string content:" << content <<endl;
string format_content = RemovePunctuation(content);
cout<< "Parsed string :" <<format_content<<endl;
return 0;
}
The string with punctuation characters and the string without the punctuation characters are displayed here on the output screen.
Conclusion
To sum up, we have covered multiple ways in C++ to remove punctuation characters from a string. You can use the above-discussed four methods for removing all punctuation characters from a string. These methods give you a filter-out punctuation string in C++. By reading the article, you can discover which of these approaches is more convenient.