Home C++ Strings Functions strpbrk() C++ strpbrk() Function 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.
strpbrk("Hello, world", " ,"); Call What it does strpbrk("Hello, world", " ,")Points at the first space or comma. Here that is the comma.
Library declarations const char* strpbrk(const char* dest, const char* breakset); Parameters Parameter Description text Required. The C-string to search. set Required. The characters that count as a match.
Return Value Return Value Description Pointer Points at the first character from the set. nullptr None of those characters appear.
C++ Examples 1. Basic find 2. Not found 3. Get index Example 1 — Basic strpbrk() Find the first separator in a sentence.
#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;
} Found: ','
From there: , C++! How It Works Scans left to right until a character is in " ,!". The first hit is the comma after Hello. Printing the pointer shows the rest of the string from there. Example 2 — No Match Missing set members return nullptr.
#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 None of x, y, or z appear. Result is nullptr. Always test before dereferencing. Example 3 — Get the Index Use pointer subtraction for a zero-based position.
#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;
} First break at index: 6
Character: '/' How It Works Set is "./" (slash or dot). First match is '/' at index 6. Useful for path and delimiter parsing. Frequently Asked Questions What does strpbrk() do in C++? strpbrk() finds the first character in a C-string that belongs to a set of characters.
What are the parameters of strpbrk()? text is the C-string to search. set is the list of characters that count as a match.
What does strpbrk() return? A pointer to the first matching character, or nullptr when none of the set appears.
How is strpbrk() different from strchr()? strchr() looks for one character. strpbrk() looks for any character from a set.
Why check for nullptr? If nothing in the set appears, there is no character to read.
About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion