The strpbrk() function finds the first character in a string that belongs to a set you specify. Think of it as searching for “any of these symbols” in one pass—useful for vowels, whitespace, delimiters, and punctuation.
01
First match
Any in set.
02
Returns ptr
Or NULL.
03
Accept set
Char list.
04
Read-only
No modify.
05
string.h
Standard C.
06
vs strcspn
Len vs ptr.
Fundamentals
Definition and Usage
strpbrk stands for string pointer break. It walks s from left to right and stops at the first byte that matches any character listed in accept. The match is a single character, not a substring—accept = "abc" means “find a, b, or c.”
The returned pointer points inside the original string. You can print the character with *result, compute an index with result - s, or continue scanning from result + 1.
💡
Beginner Tip
Always check for NULL before using the result. If strpbrk finds nothing, dereferencing the return value causes undefined behavior.
Foundation
📝 Syntax
Standard C declaration:
C
char *strpbrk(const char *s, const char *accept);
Parameters
s — null-terminated string to search.
accept — null-terminated set of characters to find (any one match stops the search).
Return Value
Pointer to the first matching character in s.
NULL if no character from accept appears in s.
Header
#include <string.h>
Cheat Sheet
⚡ Quick Reference
s
accept
Result
"Hello, C!"
"aeiou"
pointer to 'e' (index 1)
"file.txt"
"."
pointer to '.'
"ABC123"
"xyz"
NULL
index
if p != NULL
p - s (pointer subtraction)
Find set
strpbrk(s, accept)
First match
One char
strchr(s, c)
Single search
Prefix len
strcspn(s, reject)
Before match
Check
if (p != NULL)
Before use
Hands-On
Examples Gallery
Compile with gcc strpbrk.c -std=c11 -o strpbrk. Every example checks for NULL before using the returned pointer.
📚 Getting Started
Locate the first character from a small accept set.
strcspn(s, "=") returns 5—the token length before =. strpbrk returns a pointer to = itself. Use length to copy a prefix; use pointer to inspect or split at the delimiter.
Applications
🚀 Common Use Cases
Token parsing — find the first delimiter in key=value or CSV text.
Input validation — detect forbidden symbols in usernames or paths.
Text analysis — locate the first vowel, digit, or punctuation mark.
Lexer-style scans — jump to the next operator character.
Path handling — find / or \ among separator sets.
🧠 How strpbrk() Works
1
Read s[i]
Walk s left to right until '\0'.
Scan
2
Test accept set
Is s[i] equal to any char in accept?
Match
3
Return or continue
On match return &s[i]; else advance i.
Result
=
🔍
char* or NULL
Pointer into s, or no match.
Important
📝 Notes
Case-sensitive byte comparison—not locale-aware.
accept is a character set, not a substring to find as a whole.
Return pointer is into s; do not free() it.
Empty accept makes the function return NULL (no chars to match).
Pair with strcspn when you need prefix length instead of a pointer.
Performance
⚡ Optimization
Library implementations often build a lookup table from accept for faster scans. For tiny sets in hot loops, a hand-written loop can be equally clear. Profile before micro-optimizing—correct NULL checks matter more than shaving comparisons.
Wrap Up
Conclusion
strpbrk() locates the first character in a string that appears in your accept set. Check for NULL, use pointer subtraction for indexes, and combine it with strcspn() when building parsers.
Next, learn strrchr() to find the last occurrence of a single character.
Modify the string through the const source incorrectly
Confuse strpbrk with strstr (whole substring)
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about strpbrk()
Use these points when searching for any of several characters.
5
Core concepts
🔍01
Any in set
First hit.
Basics
📝02
NULL safe
Check first.
Safety
🔢03
Ptr in s
Not copy.
API
📈04
vs strcspn
Len vs ptr.
Compare
💬05
Case matters
Byte match.
Rules
❓ Frequently Asked Questions
strpbrk() scans a string s for the first character that also appears in the accept set (another string). It returns a pointer to that character inside s, or NULL if no match is found.
char* strpbrk(const char* s, const char* accept); Include <string.h>. accept is a null-terminated list of characters to search for—not a substring pattern.
It returns NULL. Always test the result before dereferencing it or computing an index.
strchr(s, c) looks for one specific character. strpbrk(s, accept) looks for the first occurrence of any character listed in accept. Use strpbrk when the match can be one of several symbols.
strpbrk returns a pointer to the first matching character (or NULL). strcspn returns the length of the prefix before any reject-set character appears. They are often used together in parsers.
Yes. 'A' and 'a' are different. For case-insensitive search, normalize case first or write a custom loop with tolower().
Did you know?
The name strpbrk is easy to misread as “string break.” Think pointer brkeak: it returns a pointer to where the scan “breaks” because a matching character was found. Our strcspn tutorial covers the complementary function that returns how many characters came before that break.