Reputation: 12621
#include<stdio.h>
#include<string.h>
int main(){
char str[] = "This is a sample string";
char *pch;
char piece1[10] = " ";
char piece2[10] = " ";
printf("\n %s \n",str);
pch = strtok(str," ");
strcpy(piece1,pch);
printf("\n piece1 : %s \n",piece1);
while(pch != NULL){
printf("\n %s \n",pch);
pch = strtok(NULL," ");
if(pch != NULL){
strcpy(piece2,pch);
printf("\n piece2 : %s \n",piece2);
}
}
printf("\n str: %s \n",str); //prints only "This" and not the original string
return 0;
}
I could not understand the second strtok
called with NULL as 1st argument. When strtok
is called with the str it splits the first word. what does the strtok(NULL," ")
, do on NULL?
When I printer str, it prints the first word and not the original string it contained before tokenising.
Upvotes: 0
Views: 2587
Reputation: 601
what strtok
does behind the scenes is put \0
wherever the delimiter character is found. This is why you pass in NULL
the subsequent times after the first call to strtok
(it keeps track of its next starting positional state).
When you print out str
it is still a pointer to the start of your original string to tokenize but the first delimiter is now a \0
. printf
only prints up to a \0
.
Upvotes: 2
Reputation: 7044
From man strtok(), see the bold highlight.
The strtok() function is used to isolate sequential tokens in a null-ter- minated string, str. These tokens are separated in the string by at least one of the characters in sep. The first time that strtok() is called, str should be specified; subsequent calls, wishing to obtain fur- ther tokens from the same string, should pass a null pointer instead. The separator string, sep, must be supplied each time, and may change between calls.
So, it changes the string in place and it keeps a variable between calls.
Upvotes: 0
Reputation: 42627
strtok
changes the input string (which is why you can't use a const/read-only string as input). Basically, at each break, it replaces the separator with \0
to terminate it.
It also stores some state globally, which is how it knows where the next item starts.
If you want to know where each word is, you need to keep track of each pointer returned from each strtok
call.
Upvotes: 1