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 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
, the behavior ofstd::ispunctis undefined if the argument's value is neither representable asunsigned charnor equal to EOF. To use these functions safely with plainchars (orsigned chars), the argument should first be converted tounsigned char.
By studying the example it provides, I am able to make it work like this:
#include
#include
#include
#include
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;
}