#include <stdio.h>
#include <string.h>
int main ()
{
char str[] = "This is an example of strtok, it will split this string into words.";
char separators[] = " ,.";
const unsigned int MAX_WORDS = 100;
char *words[MAX_WORDS];
unsigned int wordsCount = 0;
char *p = strtok( str, separators );
while ( p != NULL && wordsCount < MAX_WORDS )
{
words[ wordsCount++ ] = p;
p = strtok( NULL, separators );
}
for ( unsigned int i = 0; i < wordsCount; i++ )
{
printf( "%s\n", words[ i ] );
}
printf( "\nThere are %d word(s) in the string\n", wordsCount );
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdHJpbmcuaD4KCmludCBtYWluICgpCnsKICBjaGFyIHN0cltdID0gIlRoaXMgaXMgYW4gZXhhbXBsZSBvZiBzdHJ0b2ssIGl0IHdpbGwgc3BsaXQgdGhpcyBzdHJpbmcgaW50byB3b3Jkcy4iOwoKICBjaGFyIHNlcGFyYXRvcnNbXSA9ICIgLC4iOwoKICBjb25zdCB1bnNpZ25lZCBpbnQgTUFYX1dPUkRTID0gMTAwOwoKICBjaGFyICp3b3Jkc1tNQVhfV09SRFNdOwogIHVuc2lnbmVkIGludCB3b3Jkc0NvdW50ID0gMDsKCiAgY2hhciAqcCA9IHN0cnRvayggc3RyLCBzZXBhcmF0b3JzICk7CgogIHdoaWxlICggcCAhPSBOVUxMICYmIHdvcmRzQ291bnQgPCBNQVhfV09SRFMgKQogIHsKICAgIHdvcmRzWyB3b3Jkc0NvdW50KysgXSA9IHA7CiAgICBwID0gc3RydG9rKCBOVUxMLCBzZXBhcmF0b3JzICk7CiAgfQoKCiAgZm9yICggdW5zaWduZWQgaW50IGkgPSAwOyBpIDwgd29yZHNDb3VudDsgaSsrICkKICB7CiAgICBwcmludGYoICIlc1xuIiwgd29yZHNbIGkgXSApOwogIH0KCiAgcHJpbnRmKCAiXG5UaGVyZSBhcmUgJWQgd29yZChzKSBpbiB0aGUgc3RyaW5nXG4iLCB3b3Jkc0NvdW50ICk7CgogIHJldHVybiAwOwp9