C strpbrk() Function

Definition
In C, strpbrk() searches a C-string for the first character that belongs to a set. For example, the set " ," means a space or a comma.
You pass the text and the set. Both are required. The result is a pointer into the same string, or NULL when nothing matches. Include <string.h>.
Syntax
Pass the text, then the set of characters. Start with this call.
strpbrk("hello,world", ",;");
| Call | What it does |
|---|
strpbrk("hello,world", ",;") | Finds the first comma or semicolon. Here that is the comma. |
Include <string.h>. The second argument is a set of characters, not one phrase. Check for NULL.
Library declarations
char *strpbrk(const char *s, const char *accept);
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 to the first matching character inside the same string. |
| NULL | Returned when no character from the set is found. |
C Examples
Example 1 — Find the First Delimiter
Stop at the first comma or semicolon and print from that point.
#include <stdio.h>
#include <string.h>
int main(void) {
const char *s = "hello,world";
char *p = strpbrk(s, ",;");
if (p != NULL) {
printf("found: %c\n", *p);
printf("rest: %s\n", p);
} else {
printf("no delimiter\n");
}
return 0;
}
How It Works
- Accept set is delimiters. Either
',' or ';' can match. - Scan from the left. Letters of
"hello" are skipped. - Comma wins.
p points at the first delimiter inside s. - Print from p.
%s shows the suffix starting at that character.
Example 2 — Get the Index of the Match
Subtract pointers to turn the result into a zero-based index.
#include <stdio.h>
#include <string.h>
int main(void) {
const char *s = "code-to-fun";
char *p = strpbrk(s, "-_");
if (p != NULL) {
printf("index = %td\n", p - s);
printf("char = %c\n", *p);
}
return 0;
}
How It Works
- Search for separators. Accept contains
'-' and '_'. - First hit is '-'. It appears after
"code". - Pointer math.
p - s is the index (4). - Same idea as strcspn. That index equals
strcspn(s, "-_") when a match exists.
Example 3 — No Match Returns NULL
Always test the result before reading through the pointer.
#include <stdio.h>
#include <string.h>
int main(void) {
const char *s = "abcdef";
char *p = strpbrk(s, "xyz");
if (p == NULL) {
printf("no match\n");
} else {
printf("found %c\n", *p);
}
return 0;
}
How It Works
- Accept set unused. None of
x y z appear in s. - Scan reaches the end. No character qualifies as a hit.
- Return is NULL. There is no valid pointer into
s. - Guard before use. Skipping the
NULL check would be undefined behavior.
Frequently Asked Questions
strpbrk() finds the first character from a set and returns a pointer to it, or NULL.
Both are required. text is the string to search, and set lists the matching characters.
A pointer to the first match, or NULL when nothing matches.
Include <string.h>.
strchr() looks for one character. strpbrk() looks for any character from a set.
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.
9 people found this page helpful