C Program Example strtok function

C Program Example strtok function


  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. int main ()
  6. {
  7.   char str[] ="- This, a sample string.";
  8.   char * pch;
  9.  
  10.   printf ("Splitting string \"%s\" into tokens:\n",str);
  11.  
  12.   pch = strtok (str," ,.-");
  13.   if(pch == NULL) return -1; /* nedostatek pameti */
  14.  
  15.   while (pch != NULL)
  16.   {
  17.     printf ("%s\n",pch);
  18.  
  19.     free(pch);
  20.     pch = strtok (NULL, " ,.-");
  21.     if(pch == NULL) return -1;
  22.   }
  23.  
  24.   free(pch);
  25.   return 0;
  26. }


Learn More :