C++ strings & string member functions

I have to get input from the console for a paragraph, and break the paragraph into sentences and then words.

I'm trying to do this with the C++ string class.

I have the following code for getting the paragraph

string getParagraph() {
do {
getline(cin, sentence);
paragraph += sentence + "\n";
} while (sentence.length() >0);
}

I don't know how to break the paragraph into separate sentences, and then into separate words? I think there is a member function for the string class that leeks you peek ahead to see the next character, and the unget functions would help for this(to unget whitespace or punctuation), but I don't remember the name of the first member function.






Last edited on
I would look into:

string::find
string::substr

Run through the paragraph finding full stops - add each substring to a vector of sentences. Then run through the each sentence look for spaces and add each substring to a vector of words.

Definitely over kill for something like this - but it is worth knowing some regex. If you are just playing around & learning stuff I suggest you look into the Boost::regex library (http://www.boost.org/doc/libs/1_45_0/libs/regex/doc/html/index.html).

Nick.

(P.S if you need help writing the algorithm post your attempts here and I'll help you along the way).
Last edited on
Topic archived. No new replies allowed.