C++ Remove punctuation from String

后端 未结 12 2367
天涯浪人
天涯浪人 2020-11-29 07:02

I got a string and I want to remove all the punctuations from it. How do I do that? I did some research and found that people use the ispunct() function (I tried that), but

相关标签:
12条回答
  • 2020-11-29 07:20

    ispunct takes a char value not a string.

    you can do like

    for (auto c : string)
         if (ispunct(c)) text.erase(text.find_first_of(c));
    

    This will work but it is a slow algorithm.

    0 讨论(0)
  • 2020-11-29 07:23

    Pretty good answer by Steve314. I would like to add a small change :

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

    Adding the :: before the function ispunct takes care of overloading .

    0 讨论(0)
  • 2020-11-29 07:27
     #include <iostream>
     #include <string>
    
     using namespace std;
    
     int main()
     {
       string s;//string is defined here.
    
      cout << "Please enter a string with punctuation's: " << endl;//Asking for users input
    
      getline(cin, s);//reads in a single string one line at a time
    
    /* ERROR Check: The loop didn't run at first because a semi-colon was placed at the end 
                    of the statement.  Remember not to add it for loops. */
            for(auto &c : s)  //loop checks every character 
            {       
                 if (ispunct(c)) //to see if its a punctuation
                  {
                   c=' ';       //if so it replaces it with a blank space.(delete) 
                  }
    
            }
    
            cout <<  s << endl; 
    
    
       system("pause");
       return 0;
       }
    
    0 讨论(0)
  • 2020-11-29 07:28

    I tried to apply @Steve314's answer but couldn't get it to work until I came across this note here on cppreference.com:

    Notes

    Like all other functions from <cctype>, the behavior of std::ispunct is undefined if the argument's value is neither representable as unsigned char nor equal to EOF. To use these functions safely with plain chars (or signed chars), the argument should first be converted to unsigned char.

    By studying the example it provides, I am able to make it work like this:

    #include <string>
    #include <iostream>
    #include <cctype>
    #include <algorithm>
    
    int main()
    {
        std::string text = "this. is my string. it's here.";
        std::string result;
        text.erase(std::remove_if(text.begin(),
                                  text.end(),
                                  [](unsigned char c) { return std::ispunct(c); }),
                   text.end());
        std::cout << text << std::endl;
    }
    
    0 讨论(0)
  • 2020-11-29 07:30

    Using algorithm remove_copy_if :-

    string text,result;
    std::remove_copy_if(text.begin(), text.end(),            
                            std::back_inserter(result), //Store output           
                            std::ptr_fun<int, int>(&std::ispunct)  
                           );
    
    0 讨论(0)
  • 2020-11-29 07:33
    #include <string>
    #include <iostream>
    #include <cctype>
    
    int main() {
    
        std::string text = "this. is my string. it's here.";
    
        for (int i = 0, len = text.size(); i < len; i++)
        {
            if (ispunct(text[i]))
            {
                text.erase(i--, 1);
                len = text.size();
            }
        }
    
        std::cout << text;
        return 0;
    }
    

    Output

    this is my string its here
    

    When you delete a character, the size of the string changes. It has to be updated whenever deletion occurs. And, you deleted the current character, so the next character becomes the current character. If you don't decrement the loop counter, the character next to the punctuation character will not be checked.

    0 讨论(0)
提交回复
热议问题