C String strpbrk() Function

Beginner
⏱️ 10 min read
📚 Updated: Jul 2026
🎯 5 Examples
<string.h>

What You’ll Learn

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.

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.

📝 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>

⚡ Quick Reference

sacceptResult
"Hello, C!""aeiou"pointer to 'e' (index 1)
"file.txt""."pointer to '.'
"ABC123""xyz"NULL
indexif p != NULLp - 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

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.

Example 1 — Find the First Vowel

Search "Hello, C Programming!" for any of aeiou.

C
#include <stdio.h>
#include <string.h>

int main(void) {
    const char s[] = "Hello, C Programming!";
    const char vowels[] = "aeiou";

    char *result = strpbrk(s, vowels);

    if (result != NULL) {
        printf("First vowel found: %c\n", *result);
    } else {
        printf("No vowel found.\n");
    }

    return 0;
}

How It Works

The scan skips H, then finds e at index 1. result points into the middle of s—not a new string.

Example 2 — Find a Delimiter Character

Locate the first comma, semicolon, or colon in a CSV-style line.

C
#include <stdio.h>
#include <string.h>

int main(void) {
    const char line[] = "name;age:30";

    char *delim = strpbrk(line, ",;:");

    if (delim != NULL) {
        printf("First delimiter '%c' at index %td\n", *delim, delim - line);
    }

    return 0;
}

How It Works

Parsers often split tokens at the first delimiter. strpbrk tells you exactly where splitting should begin.

📈 Practical Patterns

Missing matches, index math, and pairing with strcspn.

Example 3 — When No Character Matches

Handle the NULL return value safely.

C
#include <stdio.h>
#include <string.h>

int main(void) {
    const char digits[] = "12345";
    char *p = strpbrk(digits, "abcdef");

    if (p == NULL) {
        printf("No letters a-f in the string.\n");
    } else {
        printf("Found: %c\n", *p);
    }

    return 0;
}

How It Works

When the accept set never appears, strpbrk returns NULL. Never call *p without checking first.

Example 4 — Continue Scanning for the Next Match

Find multiple whitespace characters by advancing the search pointer.

C
#include <stdio.h>
#include <string.h>

int main(void) {
    const char text[] = "one two  three";
    const char *cursor = text;
    const char *spaces = " \t";
    int count = 0;

    while ((cursor = strpbrk(cursor, spaces)) != NULL) {
        count++;
        cursor++;
    }

    printf("Whitespace runs found: %d\n", count);

    return 0;
}

How It Works

After each match, increment cursor to keep searching. This pattern counts occurrences or walks through repeated separators.

Example 5 — strpbrk() vs strcspn()

Same string, two views: pointer to the match vs length before it.

C
#include <stdio.h>
#include <string.h>

int main(void) {
    const char s[] = "token=42";

    char *p = strpbrk(s, "=");
    size_t len = strcspn(s, "=");

    printf("strpbrk char: %c\n", p ? *p : '?');
    printf("strcspn len:  %zu\n", len);

    return 0;
}

How It Works

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.

🚀 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.

📝 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.

⚡ 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.

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.

💡 Best Practices

✅ Do

  • Test result != NULL before dereferencing
  • Use pointer subtraction for indexes: p - s
  • Keep accept strings short and readable (" \t\n")
  • Include <string.h>
  • Combine with strcspn for token extraction

❌ Don’t

  • Treat accept as a substring pattern
  • Dereference a NULL return value
  • Assume case-insensitive matching
  • Modify the string through the const source incorrectly
  • Confuse strpbrk with strstr (whole substring)

Key Takeaways

Knowledge Unlocked

Five things to remember about strpbrk()

Use these points when searching for any of several characters.

5
Core concepts
📝 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.

Continue the String Functions Series

Search character sets with strpbrk(), then find the last single-character match with strrchr().

Next: strrchr() →

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.

6 people found this page helpful