I'm trying to remove all punctuation characters from a std::string in C++. My current code:
string str_in; string::size_type i, j; cout << "please input string with punctuation character..." << endl; cin >> str_in; for (i = 0, j = 0; i != str_in.size(); ++i) if (!ispunct(str_in[i])) str_in[j++] = str_in[i]; str_in[j] = '\0'; cout << str_in << endl; Is str_in[j] = '\0'; wrong?
str_in[j] = '\0'is wrong since that's not an appropriate way to terminate astd::string. What is the question?std::strings are not defined to use a terminator. I think you're trying to delete all punctuation marks from astd::string, so"foo.bar+baz"becomes"foobarbaz". If so, that has nothing to do with termination.