why does strtok return substring of delimited string?

Suppose I have a character string "the pe(o)ple."
And use a char array of delimiters; char delimiters[]="()"
Why does strtok return "the pe" and not the "the people"
Last edited on
The strtok function is not just meant to remove characters from a string. It is more for when you want to extract substrings that are separated by some character (e.g. names separated by commas).

Passing the string to strtok will return the substring up to the first delimiter character. Calling strtok again, but passing NULL instead of passing the string, you get the next substring. Normally strtok is used in a loop.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <cstring>

int main()
{
	char delimiters[] = "()";
	char string[] = "the pe(o)ple.";

	for (char* token = std::strtok(string, delimiters); 
	     token != NULL; 
	     token = std::strtok(NULL, delimiters))
	{
		std::cout << '"' << token << '"' << std::endl;
	}
}
"the pe"
"o"
"ple."

http://en.cppreference.com/w/cpp/string/byte/strtok
Last edited on
Did you read the http://www.cplusplus.com/reference/cstring/strtok/

That says that the first call to strtok replaces the '(' with null and returns a pointer to 't'.
Therefore, the array contains the the pe\0o)ple.\0.

The second call (with NULL parameter) replaces the ')' with null and thus shows "o".
The third call (with NULL parameter) shows "ple.".
All subsequent calls will return NULL.
closed account (48T7M4Gy)
The delimeters are ( and ) so the sentence is broken up in tokens accordingly. When a delimiting character is reached, strtok 'breaks off' a token.

So the first token is
the pe
, second is
o
and third is
ple.


Each token is a pointer to that part 'broken off'

There ya go :)
Topic archived. No new replies allowed.