Help with my excerise

closed account (3qX21hU5)
So im in the middle of trying to teach myself c++ by reading C++ Primer the fourth edition, and im stuck on one of the exercises and could use some pointers to sort it out. If possible please dont just give the answer to the problem just tips on figuring it out would work thank you.

Exercise - Write a program to strip the punctuation from a string. The input to the the program should be a string ofcharacter including punctuation; the output should be a string in which the punctuation is removed.

This is what I have so far and where im stuck, if you see anything else out of place or misused let me know.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>

using std::cout;
using std::cin;
using std::endl;
using std::string;

int main()
{
    string s1;
    getline(cin, s1);

    for(string::size_type index = 0; index != s1.size(); ++index)
        if (ispunct(s1[index]))

        // This is where im stuck
        // Dont know how to delete the punctuation

    cout << s1 << endl;

    return 0;
}


Thanks in advance for the advice and tips
1
2
3
    for(string::size_type index = 0; index != s1.size(); ++index)
        if (ispunct(s1[index]))
            s1.erase(index, 1) ;


http://www.cplusplus.com/reference/string/string/
You can use string::erase

help is here http://www.cplusplus.com/reference/string/string/erase/
closed account (3qX21hU5)
Thanks guys for the help for the s1.erase(index, 1). The way im guessing it works is index is where it starts the delete and 1 means it deletes one char? Thats what I got from it let me know if im wrong.
It's exactly like you said. You're using the first version of the functions explained here http://www.cplusplus.com/reference/string/string/erase/
Topic archived. No new replies allowed.