Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Remove punctuation from String

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 I cant seem to get it to work in my code. Anyone got any ideas?

#include <string>

int main() {

string text = "this. is my string. it's here."

if (ispunct(text))
text.erase();

return 0;
}
like image 867
NewFile Avatar asked Oct 02 '13 14:10

NewFile


1 Answers

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)  
                       );
like image 71
P0W Avatar answered Sep 29 '22 18:09

P0W