Skip to content

Instantly share code, notes, and snippets.

@owlfox
Last active August 29, 2015 14:27
Show Gist options
  • Save owlfox/f97629228aa4e5aa87b7 to your computer and use it in GitHub Desktop.
Save owlfox/f97629228aa4e5aa87b7 to your computer and use it in GitHub Desktop.
Summer 2015 part2 Day3 quiz2, about reentrancy
#include <stdio.h>
#include <string.h>
int main(void)
{
char test[80], blah[80];
char *sep = "\\/:;=-";
char *word, *phrase, *brkt, *brkb;
strcpy(test, "This;is.a:test:of=the/string\\tokenizer-function.");
//Note strtok is obsoleted and not reentrant
for (word = strtok(test, sep);
word;
word = strtok(NULL, sep))//strtok: a function that stores the "state" of the parameter passed to it.
{
strcpy(blah, "blah:blat:blab:blag");
//strtok_r is reentrant for it stores the last state in '&brkb'
//char* strtok_r(char *restrict str, const char *restrict sep, char **restrict lasts);
for (phrase = strtok_r(blah, sep, &brkb);
phrase;
phrase = strtok_r(NULL, sep, &brkb))
{
printf("So far we're at %s:%s, brkb:%s\n", word, phrase,brkb);
}
}
return 0;
}
@owlfox
Copy link
Author

owlfox commented Aug 23, 2015

execution result
So far we're at This:blah, brkb:blat:blab:blag
So far we're at This:blat, brkb:blab:blag
So far we're at This:blab, brkb:blag
So far we're at This:blag, brkb:(null)
So far we're at is.a:blah, brkb:blat:blab:blag
So far we're at is.a:blat, brkb:blab:blag
So far we're at is.a:blab, brkb:blag
So far we're at is.a:blag, brkb:(null)
So far we're at test:blah, brkb:blat:blab:blag
So far we're at test:blat, brkb:blab:blag
So far we're at test:blab, brkb:blag
So far we're at test:blag, brkb:(null)
So far we're at of:blah, brkb:blat:blab:blag
So far we're at of:blat, brkb:blab:blag
So far we're at of:blab, brkb:blag
So far we're at of:blag, brkb:(null)
So far we're at the:blah, brkb:blat:blab:blag
So far we're at the:blat, brkb:blab:blag
So far we're at the:blab, brkb:blag
So far we're at the:blag, brkb:(null)
So far we're at string:blah, brkb:blat:blab:blag
So far we're at string:blat, brkb:blab:blag
So far we're at string:blab, brkb:blag
So far we're at string:blag, brkb:(null)
So far we're at tokenizer:blah, brkb:blat:blab:blag
So far we're at tokenizer:blat, brkb:blab:blag
So far we're at tokenizer:blab, brkb:blag
So far we're at tokenizer:blag, brkb:(null)
So far we're at function.:blah, brkb:blat:blab:blag
So far we're at function.:blat, brkb:blab:blag
So far we're at function.:blab, brkb:blag
So far we're at function.:blag, brkb:(null)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment