C++ Remove punctuation from String

后端 未结 12 2430
天涯浪人
天涯浪人 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条回答
  •  -上瘾入骨i
    2020-11-29 07:33

    #include 
    #include 
    #include 
    using namespace std;
    
    int main() {
        string str = "this. is my string. it's here.";
    
        transform(str.begin(), str.end(), str.begin(), [](char ch)
        {
            if( ispunct(ch) )
                return '\0';
            return ch;
        });
    }
    

提交回复
热议问题