Remove Punctuation From a String in C++

  1. Use std::erase and std::remove_if Functions to Remove Punctuation From String in C++
  2. Use Custom Function to Remove Punctuation From String in C++
  3. Use the Iterative Method to Remove Punctuation From String in C++

This article will explain several methods of how to remove punctuation from a string in C++.

Use std::erase and std::remove_if Functions to Remove Punctuation From String in C++

std::remove_if is part of the STL algorithms library, and it can remove all elements for which the given condition evaluates true. The condition should return the bool value for each element in the range. In this case, we pass the ispunct function to check for the punctuation symbols. Notice that std::remove_if returns a pass-the-end iterator for the new range of values, so we are chaining call to it with the erase method that results in the parsed string. This solution works similar to the erase-remove idiom.

#include <iostream>
#include <iostream>
#include <string>

using std::cout; using std::cin;
using std::endl; using std::string;

int main(){
    string text = "Lorem ipsum, dolor sit! amet, consectetur? adipiscing elit. Ut porttitor.";

    cout << text << endl;

    text.erase(std::remove_if(text.begin(), text.end(), ispunct), text.end());
    cout << text << endl;


    return EXIT_SUCCESS;
}

Output:

Lorem ipsum, dolor sit! amet, consectetur? adipiscing elit. Ut porttitor.
Lorem ipsum dolor sit amet consectetur adipiscing elit Ut porttitor

Use Custom Function to Remove Punctuation From String in C++

Alternatively, one can move the previous method to a separate function, creating a local copy of the given string and operating on it while returning the parsed value to the caller code. This function can be utilized to extend the functionality to support different character groups or even pass the custom predicate function for the third parameter of the remove_if algorithm.

#include <iostream>
#include <string>

using std::cout; using std::cin;
using std::endl; using std::string;

string removeSpaces(const string& s) {
    string tmp(s);
    tmp.erase(std::remove_if(tmp.begin(), tmp.end(), ispunct), tmp.end());
    return tmp;
}

int main(){
    string text = "Lorem ipsum, dolor sit! amet, consectetur? adipiscing elit. Ut porttitor.";

    cout << text << endl;

    string parsed_text = removeSpaces(text);
    cout << parsed_text << endl;

    return EXIT_SUCCESS;
}

Output:

Lorem ipsum, dolor sit! amet, consectetur? adipiscing elit. Ut porttitor.
Lorem ipsum dolor sit amet consectetur adipiscing elit Ut porttitor

Use the Iterative Method to Remove Punctuation From String in C++

Another alternative is to implement a separate function that iterates through every character of the string. The function takes the string by reference and utilizes the for loop to traverse the string. In each iteration, ispunct is called to check if the character is a punctuation symbol or not. Note that, len variable is assigned the string’s size on each matching condition because the original string object gets modified by the erase function, and the loop needs to renew the count.

#include <iostream>
#include <string>

using std::cout; using std::cin;
using std::endl; using std::string;

void removeSpaces2(string& s) {
    for (int i = 0, len = s.size(); i < len; i++)
    {
        if (ispunct(s[i])) {
            s.erase(i--, 1);
            len = s.size();
        }
    }
}

int main(){
    string text = "Lorem ipsum, dolor sit! amet, consectetur? adipiscing elit. Ut porttitor.";

    cout << text << endl;

    removeSpaces2(text);
    cout << text << endl;

    return EXIT_SUCCESS;
}

Output:

Lorem ipsum, dolor sit! amet, consectetur? adipiscing elit. Ut porttitor.
Lorem ipsum dolor sit amet consectetur adipiscing elit Ut porttitor
Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - C++ String

  • Tokenize a String in C++
  • Check if String Is Empty in C++