#include <iostream> #include <string> using namespace std; using std::string; int main() { string str; cout << "Enter the string: " << endl; getline(cin, str); unsigned int len = str.size(); // intitilize len by its original size for (int i = 0; i < len ; i++) // keeps incrementing if there is no punctutation { if (ispunct(str[i])) { str.erase(i, 1); // erase the element i to one place. i--; // new character takes place of the erased one. We are neglecting the effect of loop increment len = str.size(); // removing the punctutation shrinks the string size } // length size changes with every iteration and we give new len value } cout << "The word without punctutation is " << str << endl; return 0; }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter