-2

Sorry for probably a stupid question but, after reading a considerably amount of examples I still don't understand how strtok() works.

Here is example:

char s[] = "   1 2 3"; // 3 spaces before 1
int count = 0;
char* token = strtok(s, " ");
while (token != NULL) {
   count++;
   token = strtok(NULL, " ");
}

After executing count equals 3. Why? Please explain I've given detailed steps of what happens inside the call to that function.

tijko
  • 7,599
  • 11
  • 44
  • 64
Anarantt
  • 289
  • 1
  • 2
  • 10
  • ... or you could read the documentation for `strtok()`. Smells like a homework question to me. There are three values delimited by space and space is the delimiter you passed to `strtok()`. This question would fair much better from the vote-down police if you were to explain why you think it should be anything other then three? Because I cannot imagine why you are surprised. *In detail, step-by-step" would entail writing the code ot pseudo-code of the function; you could just look at an implementation of the function. – Clifford Mar 13 '16 at 19:51
  • [One example](http://www.opensource.apple.com/source/Libc/Libc-167/string.subproj/strtok.c), [and another](http://research.microsoft.com/en-us/um/redmond/projects/invisible/src/crt/strtok.c.htm). The second example is easier to follow perhaps. – Clifford Mar 13 '16 at 19:51

2 Answers2

1

Because:

http://man7.org/linux/man-pages/man3/strtok.3.html

From the above description, it follows that a sequence of two or more
contiguous delimiter bytes in the parsed string is considered to be a
single delimiter, and that delimiter bytes at the start or end of the
string are ignored.

You could also have print the consecutive tokens. Output for me: 1 2 3

jdarthenay
  • 3,062
  • 1
  • 15
  • 20
0

It is C, but C++ reference has a nice example: http://www.cplusplus.com/reference/cstring/strtok/

char s[] once initialized, points to a memory location with data: 1 2 3.

First call to strtok, with delimiter a single space, advances the pointer to point to further memory loaction, now with just: 1 2 3. Further calls increase the pointer, to point to locations of consecutive tokens.

Note: think of strtok() as a tokenizer function, iterating forward by the valid tokens. Also, printing out the current value of pch may help to understand it better (or use a debugger).

hauron
  • 4,550
  • 5
  • 35
  • 52