-1

I tried following functionality? but couldn't understand.

 #include <stdio.h> 
 #include <string.h> 

 int main() 
 { 
     char str[] = "Geeks-for-Geeks"; 
     char* token = strtok(str, "-"); 
     while (token != NULL) { 
         printf("%s\n", token); 
         token = strtok(NULL, "-"); 
     } 

     return 0; 
 } 

In above code, most importantly I am not getting the part: token = strtok(NULL, "-"); Please explain complete working and code.

  • 1
    Yout tags are wrong. Your code is not C++. You are not using string and not the STL. Please change your tag to C and rephrase the question – A M Feb 23 '20 at 09:57
  • 3
    Does this answer your question? [How does strtok() split the string into tokens in C?](https://stackoverflow.com/questions/3889992/how-does-strtok-split-the-string-into-tokens-in-c) – vicpermir Feb 23 '20 at 10:18
  • The documentation may be useful https://en.cppreference.com/w/cpp/string/byte/strtok – Alan Birtles Feb 23 '20 at 10:57

1 Answers1

0

See the documentation at http://www.cplusplus.com/reference/cstring/strtok/

When the first parameter isn't NULL it starts a new tokenization, and returns the first token. Each call after that which has NULL as the first parameter returns the next token until there are no more tokens. When there are no more tokens to return it returns NULL.

Ralph Ritoch
  • 3,260
  • 27
  • 37