/////// File: strtok.C /////// #include #include typedef char * String; String strtok(String s, const String del) { String token; int i = 0; static String spt = NULL; // where to search for next token if ( s ) spt = s; // new string if ( !spt || spt[0] == '\0' ) return(NULL); while ( spt[i] != '\0' && strchr(del, spt[i]) ) i++ ; // find beginning of token token = &spt[i]; while ( spt[i] != '\0' && !strchr(del, spt[i]) ) i++ ; // find end of token spt[i] = '\0'; // terminator in place spt = &spt[i+1]; // record position return(token); // token produced } const char *white = "\t \n\r"; // TAB SPACE NEWLINE RETURN int main() { char st1[] = "There it is."; char st2[] = "\n \t A string\n \r of 1 or more \t tokens"; char* strarr[2] = {st1, st2}; String tk; for ( int i = 0 ; i < 2 ; i++ ) // process both strings { tk = strtok(strarr[i], white); do { cout << tk << "\n"; } while ( (tk = strtok(NULL, white)) ); } return(0); }