The Wayback Machine - https://web.archive.org/web/20240214190244/https://www.geeksforgeeks.org/strpbrk-in-c/
Open In App
Related Articles

strpbrk() in C

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report
This function finds the first character in the string s1 that matches any character specified in s2 (It excludes terminating null-characters).
Syntax : 
char *strpbrk(const char *s1, const char *s2)

Parameters :
s1 : string to be scanned.
s2 : string containing the characters to match.

Return Value :
It returns a pointer to the character in s1 that 
matches one of the characters in s2, else returns NULL.
// C code to demonstrate the working of
// strpbrk
#include <stdio.h>
#include <string.h>
  
// Driver function
int main()
{
    // Declaring three strings
    char s1[] = "geeksforgeeks";
    char s2[] = "app";
    char s3[] = "kite";
    char* r, *t;
  
    // Checks for matching character
    // no match found
    r = strpbrk(s1, s2); 
    if (r != 0)
        printf("First matching character: %c\n", *r);
    else
        printf("Character not found");
  
    // Checks for matching character
    // first match found at "e"
    t = strpbrk(s1, s3);
    if (t != 0)
        printf("\nFirst matching character: %c\n", *t);
    else
        printf("Character not found");
  
    return (0);
}

                    
Output:
Character not found
First matching character: e

Practical Application This function can be used in game of lottery where the person having string with letter coming first in victory wins, i.e. this can be used at any place where first person wins.
// C code to demonstrate practical application
// of strpbrk
#include <stdio.h>
#include <string.h>
  
// Driver function
int main()
{
    // Initializing victory string
    char s1[] = "victory";
  
    // Declaring lottery strings
    char s2[] = "a23";
    char s3[] = "i22";
    char* r, *t;
  
    // Use of strpbrk()
    r = strpbrk(s1, s2);
    t = strpbrk(s1, s3);
  
    // Checks if player 1 has won lottery
    if (r != 0)
        printf("Congrats u have won");
    else
        printf("Better luck next time");
  
    // Checks if player 2 has won lottery
    if (t != 0)
        printf("\nCongrats u have won");
    else
        printf("Better luck next time");
  
    return (0);
}

                    
Output:
Better luck next time
Congrats u have won


Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!


Last Updated : 18 Sep, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads
Complete Tutorials