In the following program, strtok()
works as expected in the major part but I just can't comprehend the reason behind one finding. I have read about strtok()
that:
To determine the beginning and the end of a token, the function first scans from the starting location for the first character not contained in delimiters (which becomes the beginning of the token). And then scans starting from this beginning of the token for the first character contained in delimiters, which becomes the end of the token.
And as we know, strtok()
places a \0
at the end of each token. But in the following program, the last delimiter is a dot(.
), after which there is Toad between that dot and the quotation mark ("
). Now the dot is a delimiter in my program, but there is no delimiter after Toad, not even a white space (which is a delimiter in my program). Please clear the following confusion arising from this premise:
Why is strtok()
considering Toad as a token even though it is not between 2 delimiters? This is what I read about strtok()
when it encounters a NULL character (\0
):
Once the terminating null character of str has been found in a call to strtok, all subsequent calls to this function with a null pointer as the first argument return a null pointer.
Nowhere does it say that once a null character is encountered,a pointer to the beginning of the token is returned (we don't even have a token here as we didn't get an end of the token as there was no delimiter character found after the scan begun from the beginning of the token (i.e. from 'T' of Toad), we only found a null character, not a delimiter). So why is the part between last delimiter and quotation mark of argument string considered a token by strtok()
? Please explain this.
Code:
#include <stdio.h>
#include <string.h>
int main ()
{
char str[] =" Falcon,eagle-hawk..;buzzard,gull..pigeon sparrow,hen;owl.Toad";
char * pch=strtok(str," ;,.-");
while (pch != NULL)
{
printf ("%s\n",pch);
pch = strtok (NULL, " ;,.-");
}
return 0;
}
Output:
Falcon
eagle
hawk
buzzard
gull
pigeon
sparrow
hen
owl
Toad
Toad
would not be printed? Going by that logic if you remove the leading space in the input string,Falcon
shouldn't be printed either. I would say that makes for some unintuitive behavior. – Cetanestrtok()
would still consider 'Falcon' to be the first token. – Giraldastrtok()
,except the last token,which is clearly not between two delimiters. – MadderFalcon
to be printed?I have mentioned from the source thatthe function first scans from the starting location for the first character not contained in delimiters
..ie,for the beginning of the token we don't need a delimiter(space is a delimiter in my program),but to mark the end of the token we clearly need a delimiter,and NULL at the string end is not on the delimiter list. – Madder