C++ examples for STL:string
Replace punctuation marks with spaces and uses C-string function strtok to tokenize string into individual words.
#include <cstring> #include <iostream> #include <string> int main(int argc, const char *argv[]) { const char char1 = '!'; const char char2 = ' '; std::cout << "Enter a string: "; std::string base;//from ww w. j ava 2s.c o m std::getline(std::cin, base); size_t pos = base.find(char1, 0); while (pos != std::string::npos) { base[pos] = char2; pos = base.find(char1, pos + 1); } // duplicate then tokenize string char *dup = strdup(base.c_str()); char *pch = strtok(dup, " "); while (pch != nullptr) { std::cout << pch << std::endl; pch = strtok(nullptr, " "); } free(dup); return 0; }