fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. int main ()
  5. {
  6. char str[] = "This is an example of strtok, it will split this string into words.";
  7.  
  8. char separators[] = " ,.";
  9.  
  10. const unsigned int MAX_WORDS = 100;
  11.  
  12. char *words[MAX_WORDS];
  13. unsigned int wordsCount = 0;
  14.  
  15. char *p = strtok( str, separators );
  16.  
  17. while ( p != NULL && wordsCount < MAX_WORDS )
  18. {
  19. words[ wordsCount++ ] = p;
  20. p = strtok( NULL, separators );
  21. }
  22.  
  23.  
  24. for ( unsigned int i = 0; i < wordsCount; i++ )
  25. {
  26. printf( "%s\n", words[ i ] );
  27. }
  28.  
  29. printf( "\nThere are %d word(s) in the string\n", wordsCount );
  30.  
  31. return 0;
  32. }
Success #stdin #stdout 0s 3456KB
stdin
Standard input is empty
stdout
This
is
an
example
of
strtok
it
will
split
this
string
into
words

There are 13 word(s) in the string