Eliminiating special cases in strtok loops
Posted Sun, 17 Jun 2007
strtok has a "first case" and "other case" usage. The first time you call
strtok, you pass it the string. Future calls must pass NULL for this same
session. This leads to this kind of code:
void foo(char *mystr) {
char *tok;
tok = strtok(mystr, " ");
while (tok != NULL) {
// Do something with tok
tok = strtok(NULL, " ");
}
}
Notice the above duplicate code. You can use pointers properly and achieve this same result with only one line using strtok:
void foo(char *mystr) {
char *tok;
char *strptr = mystr;
while ( (tok = strtok(strptr, " ")) != NULL ) {
if (strptr != NULL)
strptr = NULL;
// Do something with tok
}
}
This method lets you still invoke both first and nonfirst cases of strtok but
you only have one line of code using strtok, making your code more maintainable
and readable. This way has the great benefit of being able to use 'continue'
inside your loop and you still move on to the next token.