![]() |
VOOZH | about |
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:
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 :
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.
// 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 :
Geeks for Geeks
Time Complexity: O(n ) where n is the length of string.
Auxiliary Space: O(1).
Geeks for geeks Contribute
Time Complexity: O(n ) where n is the length of string.
Auxiliary Space: O(1).
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() :
Geeks for Geeks
Time Complexity: O(n ) where n is the length of string.
Auxiliary Space: O(1).
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:
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)