That's the way strtok
works. It's certainly not the most convenient interface in the world, but at least it is reasonably well-documented.
You seem to assume that strtok(…, "//");
is using the string "//"
as a delimiter. But the second argument to strtok
is not a single, possibly multicharacter delimiter. It's a set of possible single delimiter characters, any or all of which will be treated as delimiters. See the strtok manpage for details.
You'll find a lot of warnings about strtok
floating around the web, many of them justified. But the "gotcha" in your code is one which doesn't get mentioned much, having to do with what happens when you call strtok
twice with different delimiter sets.
To recap, strtok
first skips delimiter characters, then skips non-delimiter characters, then overwrites the next delimiter character (if there is one) with a 0. It saves the address of the next character for a subsequent call.
That works as expected when the delimiter set doesn't change. And it works as expected on strings without repeated delimiters. But there is an oddity when the string has repeated delimiters and the delimiter set is changed for the second token. The unexpected result (in your case, a mysterious /
) results from the fact that the first call to strtok
only removes the first delimiter character following the token. If the token is followed by a sequence of delimiter characters, the next call to strtok
needs to skip over the remainder of this sequence. Of course, it will do that if the delimiter set hasn't changed. But if you change the delimiter set for the second call, then what were considered delimiters from the first call are no longer consider delimiters in the second call, and they are not skipped, possibly contrary to expectations.
My guess is that strtok
is not really the best standard library function for what you are trying to do. I'd suggest thinking about a combination of the following:
strstr
to find multicharacter strings, like //
.
strchr
to find the next occurrence of a specific character.
strpbrk
to find the next occurrence of one of a set of characters.
strspn
and strcspn
to find the length of a sequence of a set of characters (or the inverse of a set of characters).
Note that the return values of the first three functions differ from the return values of the last two. The first three functions give you a pointer to the substring/character being searched for, or NULL if the search fails. The last two functions count the number of characters up to a member of the set; if they don't find one, they just return the number of characters left in the string. (This behaviour is often easier to work with, which is why the functions were added to the C standard.)
Note: You also need to fix the buffer overruns identified by @chux. You must always be conscious of the need to allocate room for a string's terminating NUL (0). But if you rewrite the code to avoid using strtok
, you might also find that you need fewer string copies, possibly even none.