C strpbrk() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
string.h

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.

C
strpbrk("hello,world", ",;");
CallWhat 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
C
char *strpbrk(const char *s, const char *accept);

Parameters

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

Return Value

Return ValueDescription
PointerPoints to the first matching character inside the same string.
NULLReturned 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.

C
#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

  1. Accept set is delimiters. Either ',' or ';' can match.
  2. Scan from the left. Letters of "hello" are skipped.
  3. Comma wins. p points at the first delimiter inside s.
  4. 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.

C
#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

  1. Search for separators. Accept contains '-' and '_'.
  2. First hit is '-'. It appears after "code".
  3. Pointer math. p - s is the index (4).
  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.

C
#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

  1. Accept set unused. None of x y z appear in s.
  2. Scan reaches the end. No character qualifies as a hit.
  3. Return is NULL. There is no valid pointer into s.
  4. 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.

Next: strrchr()

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

strrchr() tutorial →

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.

9 people found this page helpful