strpbrk : <string.h> library function

The function char *strpbrk(const char *str1, const char *str2); searches for the first occurrence of any character of string str2 in string str1. Search does not include terminating null character. If no character of str2 found in str1, then it returns NULL.

Function prototype of strpbrk

char *strpbrk(const char *str1, const char *str2);
  • str1 : This is the string to be scanned.
  • str2 : This is the string containing a list of characters to match.

Return value of strpbrk

This function returns a pointer to the first character in str1 that matches with any of the characters of str2, or a NULL pointer if none of the characters of str2 is found in str1.

C program to show the use of strpbrk function

The following program shows the use of strpbrk function to search characters in a string.

#include 
#include 
#include 

int main()
{
   char firstString[100], secondString[100];
   char *ptr;
   
   printf("Enter first string\n");
   scanf("%s", &firstString);
   printf("Enter second string\n");
   scanf("%s", &secondString);
   
   ptr = strpbrk(firstString, secondString);

   printf("First matching character is %c at index %d\n",
        *ptr, ptr-firstString);
   
   getch();
   return(0);
}

Program Output
Enter first string
asdfgAqJNE
Enter second string
MNJA
First matching character is A at index 5