Split String by Space in C++

  1. Use std::string::find and std::string::substr Functions to Split String by Space in C++
  2. Use std::istringstream and std::copy to Split String by Space in C++
  3. Use std::getline and erase-remove Idiom to Split String by Space in C++

This article will demonstrate multiple methods about how to split string by space delimiter in C++.

Use std::string::find and std::string::substr Functions to Split String by Space in C++

find and substr are std::string builtin functions that can be utilized to split string by any delimiter specified by the string value or a single character. The find function takes a string argument and returns the position on which the given substring starts; otherwise, if not found, string::npos is returned. Thus, we iterate in the while loop until the find function returns npos. Meanwhile, the substr method can be utilized to access the part of the string before the delimiter, which in this case is a single space character and store into a vector for later usage. After that, we call the erase function to remove the first sequence, including the delimiter, at which point a new iteration may proceed to repeat the operations.

#include <iostream>
#include <string>
#include <sstream>
#include <iterator>
#include <vector>
#include <algorithm>

using std::cout; using std::cin;
using std::endl; using std::string;
using std::vector; using std::istringstream;
using std::stringstream;

int main(){
    string text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. "
                  "Sed laoreet sem leo, in posuere orci elementum.";
    string space_delimiter = " ";
    vector<string> words{};

    size_t pos = 0;
    while ((pos = text.find(space_delimiter)) != string::npos) {
        words.push_back(text.substr(0, pos));
        text.erase(0, pos + space_delimiter.length());
    }
    for (const auto &str : words) {
        cout << str << endl;
    }

    return EXIT_SUCCESS;
}

Output:

Lorem
ipsum
dolor
sit
amet,
consectetur
adipiscing
elit.
Sed
laoreet
sem
leo,
in
posuere
orci

Use std::istringstream and std::copy to Split String by Space in C++

Alternatively, we can reimplement the code using the istringstream class, which provides input/output operations for string based streams. Once we initialize the istringstream object with the string value that needs to be split, then the std::copy algorithm can be called to output each space-separated string value to the cout stream. Note that this method only supports the space delimiter splitting because that’s what the implementation of the istringstream class provides.

#include <iostream>
#include <string>
#include <sstream>
#include <iterator>
#include <vector>
#include <algorithm>

using std::cout; using std::cin;
using std::endl; using std::string;
using std::vector; using std::istringstream;
using std::stringstream;

int main(){
    string text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. "
                  "Sed laoreet sem leo, in posuere orci elementum.";
    vector<string> words{};

    istringstream iss(text);
    copy(std::istream_iterator<string>(iss),
         std::istream_iterator<string>(),
         std::ostream_iterator<string>(cout, "\n"));

    return EXIT_SUCCESS;
}

Use std::getline and erase-remove Idiom to Split String by Space in C++

One downside of the previous solution is the punctuation symbols stored with parsed words. It may be solved with the erase-remove idiom, which is essentially a conditional removal operation for the given range. In this case, we call this method on each word retrieved by std::getline to trim all punctuation symbols in it. Note that the ispunct function object is passed as the third argument to the remove_if algorithm to check for the punctuation characters.

#include <iostream>
#include <string>
#include <sstream>
#include <iterator>
#include <vector>
#include <algorithm>

using std::cout; using std::cin;
using std::endl; using std::string;
using std::vector; using std::istringstream;
using std::stringstream;

int main(){
    string text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. "
                  "Sed laoreet sem leo, in posuere orci elementum.";
    char space_char = ' ';
    vector<string> words{};

    stringstream sstream(text);
    string word;
    while (std::getline(sstream, word, space_char)){
        word.erase(std::remove_if(word.begin(), word.end(), ispunct), word.end());
        words.push_back(word);
    }

    for (const auto &str : words) {
        cout << str << endl;
    }

    return EXIT_SUCCESS;
}

Output:

Lorem
ipsum
dolor
sit
amet
consectetur
adipiscing
elit
Sed
laoreet
sem
leo
in
posuere
orci
elementum
Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - C++ String

  • Trim a String in C++
  • Convert String to Uppercase in C++