| fg8ty64 (7) | |||
Hi. I've written a program for a beginner C++ class that asks the user to enter a generic five-word phrase (with a single space between). It then outputs each word of the phrase separately. The program runs fine and doesn't terminate or skip words if the user accidentally enters a double space. But my instructions say to use various string manipulation functions. How do I add those to my program? Here is the program:
| |||
|
|
|||
| Duoas (6752) | |||
Start with
Now take a look through the string class's methods http://www.cplusplus.com/reference/string/string/ to see what you can use to split the individual words out of the five_word_phrase string. Hope this helps. [edit] Hint: you will need two indices into the string: where the next word starts and where it ends. Find those locations, then you can use another method to get only that substring. | |||
|
Last edited on
|
|||
| fg8ty64 (7) | |||
I've been reading the reference site you suggested and also a book that I have, and I've come up with:
, but I'm going about it all wrong. I still don't know how to split the words and get them to output separately. I know it has something to do with where the cursor is and how it reads spaces, but don't know how to write it. Also, I don't know what the cout statement would be. | |||
|
|
|||
| Mitsakos (343) | ||
The getline() function reads a line, not just a word. So by saying getline so many times you just put your program to read more lines... So you just need one getline() that will store all the words in a single string.
When you have a phrase, like "This is a phrase", what is the key to separate each word? It is like reading it. | ||
|
|
||