A. Takami A. Takami - 9 months ago 57
C++ Question

Taking Each Individual Word From a String in C++

I am writing a method in C++ which will take a string of 2 or more words and output each individual word of the string separated by a second or so, using the

method. I am trying to do this using a for loop and substrings. I am unsure also of the regexs which should be used, and how they should be used, to achieve the desired output.

I have reviewed this and this and find my question differs since I am trying to do this in a loop, and not store the individual substrings.


"This is an example"

Desired output:

"This " (pause) "is " (pause) "an " (pause) "example."


Use std::stringstream, no regular expressions required:

#include <iostream>
#include <sstream>
using namespace std;

int main() {
    stringstream ss("This is a test");
    string s;

    while (ss >> s) {
        cout << s << endl;

    return 0;

Also, see How do I tokenize a string in C++?