/////// Separate tokens in a string /////// #include #include typedef char *String; String strtok(String s, const char *cs) { String token; static String spt; // starting pointer if( s != NULL ) spt = s; if( *spt == '\0' ) return(NULL); // locate beginning of token while( *spt != '\0' && strchr(cs, *spt) != NULL ) spt++; token = spt; // locate end of token while( *spt != '\0' && strchr(cs, *spt) == NULL ) spt++; *spt = '\0' ; spt++; return(token); } #define WHITE "\t \n\r" #ifdef TEST int main() { char st1[] = "There it is."; char st2[] = "\n \t A string\n \r of 1 or more \t tokens"; String tk = strtok(st1, WHITE); do { cout << tk << endl; } while ( (tk = strtok(NULL, WHITE)) != NULL); tk = strtok(st2, WHITE); do { cout << tk << endl; } while ( (tk = strtok(NULL, WHITE)) != NULL); return(0); } #endif