Split a String using Delimiter in C++

In this article, we will discuss different ways to split a string using delimiter in C++.

Table Of Contents

Method 1: Using a loop and string.find()

In C++, the std::string::find() function is a member function of the std::string class that searches for a specified substring within a given string and returns the position of the first occurrence of the substring. If the substring is not found, it returns std::string::npos, which is a special value defined as the maximum value of the size_t data type.

Algorithm

  1. Define a function split that takes a string and a delimiter as arguments and returns a vector of strings.
  2. Initialize a vector of strings called tokens to store the resulting tokens.
  3. Initialize a variable pos to 0. This variable will keep track of the starting position of the current token.
  4. Use a while loop to iterate until the end of the string is reached. Inside the loop:
  5. Use the find function to search for the delimiter in the string, starting from the position stored in pos. If the delimiter is found, store its position in a variable next. If the delimiter is not found, set the variable next to the length of the string.
  6. Extract the token from the string by calling the substr function with pos and next-pos as arguments. This will return a substring of the string that starts at position pos and has a length of next-pos.
  7. If the token is not empty (i.e., if it is not just the delimiter), add it to the tokens vector.
  8. Set pos to next + delim.length(), so that the next iteration of the loop will start at the position immediately following the delimiter.
  9. Return the tokens vector as the result of the split function.

Program

Advertisements
#include <iostream>
#include <string>
#include <vector>

using namespace std;

vector<string> split(const string& str, const string& delim) 
{
    vector<string> tokens;
    size_t pos = 0;
    while (pos < str.length()) 
    {
        // Find the position of the next occurrence of the delimiter
        size_t next = str.find(delim, pos);
        if (next == string::npos) 
        {
            next = str.length();
        }
        // Extract the token from the string
        string token = str.substr(pos, next-pos);
        if (!token.empty())
        {
            tokens.push_back(token);
        }
        // Update the position to start searching from the next character
        pos = next + delim.length();
    }
    return tokens;
}

int main() 
{
    string str = "Hello, world! How are you today?";
    string delim = " ";
    vector<string> tokens = split(str, delim);
    for (string token : tokens) 
    {
        cout << token << endl;
    }
    return 0;
}

Output

Hello,
world!
How
are
you
today?

Time Complexity – O(N + M)

N is the length of the string being split and M is the number of times the find function is called.
This is because pos is updated in each iteration and the find function starts searching for the delimiter from that position.

Method 2: Using stringstream()

To split a string using a delimiter in C++ using stringstream, you can use the getline function in combination with a loop.

Algorithm

  1. Define a function split that takes a string and a delimiter as arguments and returns a vector of strings.
  2. Initialize a stringstream object called ss with the string to be split.
  3. Initialize a string called token to store the current token being extracted from the stringstream.
  4. Initialize a vector of strings called tokens to store the resulting tokens.
  5. Use a while loop to iterate until the stringstream is empty. Inside the loop, use the getline function to read a token from the stringstream using the delimiter. Add the token to the tokens vector.
  6. Return the tokens vector as the result of the split function.

Program

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

using namespace std;

vector<string> split(const string& str, const char& delim) 
{
    stringstream ss(str);
    string token;
    vector<string> tokens;
    while (getline(ss, token, delim)) 
    {
        tokens.push_back(token);
    }
    return tokens;
}

int main() 
{
    string str = "Hello, world! How are you today?";
    char delim = ' ';
    vector<string> tokens = split(str, delim);
    for (string token : tokens) 
    {
        cout << token << endl;
    }
    return 0;
}

Output

Hello,
world!
How
are
you
today?

Time Complexity – O(N)

  • N is the length of the input string.
  • This is because the getline function reads the string one character at a time, so the time it takes to split the string increases linearly with the length of the string.

Method 3: Using strtok()

The strtok() is a C function that can be used to split a string using a delimiter. It modifies the original string by replacing the delimiter with a null character, so you need to be careful when using it. Here is an example of how to use strtok to split a string in C++:

Algorithm

  1. Initialize vector tokens to store the split tokens.
  2. Call strtok to split the string into tokens using the delimiter.
  3. Add the first token to the tokens vector.
  4. While there are more tokens:
  5. Call strtok to get the next token.
  6. Add the token to the tokens vector.
  7. Return the tokens vector.

Program

#include <cstring>
#include <iostream>
#include <vector>

using namespace std;

vector<string> split(char* str, char delimiter) 
{
  vector<string> tokens;
  char* token = strtok(str, &delimiter);
  while (token != NULL) 
  {
    tokens.push_back(token);
    token = strtok(NULL, &delimiter);
  }
  return tokens;
}

int main() 
{
  char str[] = "This is a test string";
  vector<string> tokens = split(str, ' ');
  for (const string& token : tokens) 
  {
    cout << token << endl;
  }
  return 0;
}

Output

This
is
a
test
string

Time Complexity – O(N)

N is the length of the input string.
The function first calls strtok to split the string into tokens, and then it iterates through the string until it reaches the end, which takes O(n) time.

Note

The strtok() function is not thread-safe, so you should avoid using it in multithreaded programs. Also, strtok() can only handle single-character delimiters, so if you want to use a multi-character delimiter, you will need to use a different method mentioned above.

Summary

In this article, we learned how to split a string by the input delimiter using string.find() function, stringstream class and strlok() function.

Advertisements

Thanks for reading.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top