VOOZH about

URL: https://www.geeksforgeeks.org/cpp/tokenizing-a-string-cpp/

⇱ Tokenizing a string in C++ - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Tokenizing a string in C++

Last Updated : 11 Jan, 2025

Tokenizing a string denotes splitting a string with respect to some delimiter(s). There are many ways to tokenize a string.

In this article four of them are explained:

Using stringstream

A stringstream associates a string object with a stream allowing you to read from the string as if it were a stream.

Below is the C++ implementation : 


Output
GeeksForGeeks
is
a
must
try

Time Complexity: O(n ) where n is the length of string.
Auxiliary Space: O(n-d) where n is the length of string and d is the number of delimiters.

Using strtok()

// Splits str[] according to given delimiters.
// and returns next token. It needs to be called
// in a loop to get all tokens. It returns NULL
// when there are no more tokens.
char * strtok(char str[], const char *delims);

Below is the C++ implementation : 


Output
Geeks
for
Geeks

Time Complexity: O(n ) where n is the length of string.
Auxiliary Space: O(1).

Another Example of strtok() :


Output
 Geeks 
 for 
 geeks 
 Contribute

Time Complexity: O(n ) where n is the length of string.
Auxiliary Space: O(1).

Using strtok_r()

Just like strtok() function in C, strtok_r() does the same task of parsing a string into a sequence of tokens. strtok_r() is a reentrant version of strtok().

There are two ways we can call strtok_r() 

// The third argument saveptr is a pointer to a char * 
// variable that is used internally by strtok_r() in
// order to maintain context between successive calls
// that parse the same string.
char *strtok_r(char *str, const char *delim, char **saveptr);


Below is a simple C++ program to show the use of strtok_r() : 


Output
Geeks
for
Geeks

Time Complexity: O(n ) where n is the length of string.
Auxiliary Space: O(1).

Using std::sregex_token_iterator

In this method the tokenization is done on the basis of regex matches. Better for use cases when multiple delimiters are needed.

Below is a simple C++ program to show the use of std::sregex_token_iterator:


Output
Break
string
a
spaces
and
commas

Time Complexity:O(n * d) where n is the length of string and d is the number of delimiters.
Auxiliary Space:O(n)

Comment
Article Tags: