Skip to content

July 18, 2014

Split a C++ String Into Words by Spaces or End of Lines

by noise

Next example shows you how to split a string into multiple words delimited by spaces or end of line (main.cpp).

#include <iostream>
#include <string>
#include <sstream>
 
using namespace std;
 
int main(int argc, const char * argv[]) {
 
    string str1("the brown fox jumped over the lazy dog");
    istringstream iss(str1);
 
    while (iss) {
        string word;
        iss >> word;
        cout << word << endl;
    }
    return 0;
}
Read more from C and C++ Examples

Leave a Reply

required
required

Note: HTML is allowed. Your email address will never be published.

Subscribe to comments