Example: strtok

In this example, strtok extracts the components of a file path. Multiple calls to strtok extract each successive component from the string, path.

The separators define how a token is demarcated i.e., they are used by strtok to find breaks between the tokens. The separators here are either `\' or `.'

When using multiple calls to strtok on the same string, specify the string only on the first invocation. Subsequent calls must pass NULL as the string parameter, as we do below.

    #include <string.h> 
    char path[] = "c:\\mercury\\lrun\\bin\\wlrun.exe";
    char separators[] = ".\\"; 
    char * token;
// Get the first token 
    token = (char *)strtok(path, separators); 
    if (!token) {
        lr_output_message ("No tokens found in string!");
        return( -1 );
// While valid tokens are returned
    while (token != NULL ) { 
        lr_output_message ("%s", token );
// Get the next token
        token = (char *)strtok(NULL, separators); 
Action.c(18): c:
Action.c(18): mercury
Action.c(18): lrun
Action.c(18): bin
Action.c(18): wlrun
Action.c(18): exe