C++ strtok() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
<cstring>

Definition

In C++, strtok() splits a mutable C-string into tokens separated by delimiter characters.

Include <cstring>.

This guide shows the C library helper in a C++ .cpp program with std::cout. Prefer std::string for new text code; keep these APIs for C interop and interviews.

Syntax

First call passes the string; later calls pass nullptr.

C++
char* t = std::strtok(buf, ",");
while (t) {
    t = std::strtok(nullptr, ",");
}
CallWhat it does
std::strtok(str, delim)First token; mutates str.
std::strtok(nullptr, delim)Next token using saved state.

POSIX offers strtok_r for reentrancy. C++ code often uses streams or string find loops instead.

Library declarations
C++
char* strtok(char* str, const char* delim);

Parameters

ParameterDescription
strMutable buffer on the first call; nullptr afterward.
delimSet of delimiter characters.

Return Value

Return ValueDescription
char*Pointer to the next token, or nullptr when done.

C++ Examples

Example 1 — Basic strtok()

Split on commas.

C++
#include <cstring>
#include <iostream>

int main() {
    char buf[] = "a,b,c";
    char* t = std::strtok(buf, ",");
    while (t) {
        std::cout << t << ' ';
        t = std::strtok(nullptr, ",");
    }
    std::cout << '\\n';
    return 0;
}

How It Works

  1. First call receives the mutable buffer.
  2. Each later call passes nullptr.
  3. Tokens print separated by spaces.

Example 2 — Several Delimiters

Split on comma or space.

C++
#include <cstring>
#include <iostream>

int main() {
    char buf[] = "one, two";
    char* t = std::strtok(buf, ", ");
    std::cout << t << '\\n';
    t = std::strtok(nullptr, ", ");
    std::cout << t << '\\n';
    return 0;
}

How It Works

  1. Delimiter string lists both comma and space.
  2. Leading delimiters between tokens are skipped.
  3. Two tokens are printed.

Example 3 — Buffer After strtok

Original commas become nulls.

C++
#include <cstring>
#include <iostream>

int main() {
    char buf[] = "x,y";
    std::strtok(buf, ",");
    std::cout << buf << '\\n';
    std::cout << (int)buf[1] << '\\n';
    return 0;
}

How It Works

  1. After the first strtok, printing buf stops at the inserted null.
  2. Index 1 holds 0.
  3. This shows the destructive behavior.

Frequently Asked Questions

It splits a mutable C-string into tokens using delimiter characters.
It mutates the buffer and stores hidden state—not thread-safe.
No. Literals are read-only. Copy into a char array first.
strtok_r on POSIX, or write your own splitter with std::string.
<cstring>.

Next: strxfrm()

Keep going through the string functions. Transform a C-string for locale-sensitive comparison with strcmp.

strxfrm() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful