C++ strtok()

The strtok() function in C++ returns the next token in a null terminated byte string.

strtok() prototype

char* strtok( char* str, const char* delim );

The strtok() function takes two arguments: str and delim. This function finds the token in the string pointed to by strtok. The pointer delim points to the separator characters.

This function can be called multiple times to obtain tokens from the same string. There are two cases:

  • If str is not NULL:
    A call to strtok() is considered first call for that string. The function searches for the first character that is not contained in delim. If no such character is found, the string does not contain any token. So a null pointer is returned. If such character is found, from there on the function searches for a character that is present in delim. If no separator is found, str has only one token. If a separator is found, it is replaced by '\0' and the pointer to the following character is stored in a static location for subsequent invocations. Finally, the function returns the pointer to the beginning of the token.
  • If str is NULL:
    The call is considered as subsequent calls to strtok and the function continues from where it left in previous invocation.

It is defined in <cstring> header file.

strtok() Parameters

  • str: Pointer to the null terminated byte string to tokenize.
  • delim: Pointer to the null terminated byte string that contains the separators.

strtok() Return value

The strtok() function returns the pointer to the next token if there is any, or it returns NULL if no more tokens are found.

Example: How strtok() function works

#include <cstring>
#include <iostream>

using namespace std;

int main()
    char str[] = "parrot,owl,sparrow,pigeon,crow";
    char delim[] = ",";
    cout << "The tokens are:" << endl;
    char *token = strtok(str,delim);
    while (token)
        cout << token << endl;
        token = strtok(NULL,delim);
    return 0;

When you run the program, the output will be:

The tokens are: