C++ strpbrk() Function

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

Definition

In C++, strpbrk() searches a C-string for the first character that belongs to a set you pass. A set such as " ," means "a space or a comma." Any one of those characters is a match.

Both arguments are required. A match is a pointer into the same string. If none of the set appears, the result is nullptr. strchr() looks for one character. strpbrk() looks for any character from the set. Include <cstring>.

Syntax

The second string is the set. Any one of those characters counts as a match.

C++
strpbrk("Hello, world", " ,");
CallWhat it does
strpbrk("Hello, world", " ,")Points at the first space or comma. Here that is the comma.
Library declarations
C++
const char* strpbrk(const char* dest, const char* breakset);

Parameters

ParameterDescription
textRequired. The C-string to search.
setRequired. The characters that count as a match.

Return Value

Return ValueDescription
PointerPoints at the first character from the set.
nullptrNone of those characters appear.

C++ Examples

Example 1 — Basic strpbrk()

Find the first separator in a sentence.

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

int main() {
    const char* text = "Hello, C++!";
    const char* seps = " ,!";

    const char* found = std::strpbrk(text, seps);

    if (found != nullptr) {
        std::cout << "Found: '" << *found << "'\n";
        std::cout << "From there: " << found << '\n';
    }
    return 0;
}

How It Works

  1. Scans left to right until a character is in " ,!".
  2. The first hit is the comma after Hello.
  3. Printing the pointer shows the rest of the string from there.

Example 2 — No Match

Missing set members return nullptr.

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

int main() {
    const char* text = "CodeToFun";
    const char* found = std::strpbrk(text, "xyz");

    if (found == nullptr) {
        std::cout << "No separator found\n";
    }
    return 0;
}

How It Works

  1. None of x, y, or z appear.
  2. Result is nullptr.
  3. Always test before dereferencing.

Example 3 — Get the Index

Use pointer subtraction for a zero-based position.

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

int main() {
    const char* path = "folder/file.txt";
    const char* found = std::strpbrk(path, "./");

    if (found != nullptr) {
        std::size_t index = static_cast<std::size_t>(found - path);
        std::cout << "First break at index: " << index << '\n';
        std::cout << "Character: '" << *found << "'\n";
    }
    return 0;
}

How It Works

  1. Set is "./" (slash or dot).
  2. First match is '/' at index 6.
  3. Useful for path and delimiter parsing.

Frequently Asked Questions

strpbrk() finds the first character in a C-string that belongs to a set of characters.
text is the C-string to search. set is the list of characters that count as a match.
A pointer to the first matching character, or nullptr when none of the set appears.
strchr() looks for one character. strpbrk() looks for any character from a set.
If nothing in the set appears, there is no character to read.

Next: strrchr()

Find the last occurrence of a character in a C-string.

strrchr() 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.

8 people found this page helpful