Removing punctuations off from a string

I'm working on a problem in which I've to design a program in which the punctuations should be removed from the string. For eg., if input str is: "Hello!!"; the output must be: "Hello".

I'm not sure how to remove a sub-string (if that's the right word!!) from a string. So, I designed a program which print out the punctuations. For eg., if input str is: "Hey!!"; the output would be: ! !

Here it is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <string>
using namespace std;
int main ()
{
    cout << "Enter a string" << endl;
    string str;
    cin >> str;
    for (string::size_type index = 0; index != str.size(); ++index)
        if (ispunct (str[index]))
        cout << str[index] << endl;
    system ("pause"); 
    return 0;
}


So, I want to know what should be added to this program so that the punctuations can be removed; or should I rewrite another program for that? Kindly help me...
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <string>

int main()
{
    std::string to_replace = "Hello!!";

    std::string punctuation = "!.,;:"; // Add more if needed
    size_t n = 0;
    while((n = to_replace.find_first_of(punctuation, n)) != std::string::npos)
        to_replace.erase(n, 1);
    std::cout << to_replace;
}
http://ideone.com/qAzb8Z
Last edited on
Topic archived. No new replies allowed.