strpbrk in C language

The C strpbrk function is a String Function, which is used to find the first character in a first string that matches any character in a second string. The syntax of the strpbrk in C Programming language is

char *strpbrk(const char *str1, const char *str2);

or we can simply write strpbrk as:

strpbrk(str1, str2);

strpbrk in C language Example

This program will help you to understand the strpbrk with multiple examples.

You must include the #include<string.h> header before using this strpbrk String Function.

#include <stdio.h> 
#include<string.h>
 
int main()
{
   	char str1[50] = "abcdcefgdhiejk";
   	char str2[50] = "ce";
   	char *result;
	
   	result = strpbrk(str1, str2);
   	
   	if(result)
   	{
   		printf("\n The First Matching Character = %c", *result);	
	}
	else
	{
		printf("\n We haven't found the Character");	
	}
}
strpbrk in C language Example 1

strpbrk in C Example 2

Instead of predefining the two strings, this C program allows the user to enter string 1 and string 2. Next, C Program will find the first records from string 1 that matches any character in string 2

#include <stdio.h> 
#include<string.h>
 
int main()
{
   	char str1[50], str2[50];
   	char *result;
	
	printf("\n Please Enter any String  : ");
	gets(str1);	
	
	printf("\n Please Enter the String that you want to Match : ");
	gets(str2);	
	
   	result = strpbrk(str1, str2);
   	
   	if(result)
   	{
   		printf("\n The First Matching Character = %c", *result);	
	}
	else
	{
		printf("\n We haven't found the Character");	
	}
}
 Please Enter any String  : tutorial gateway inside

 Please Enter the String that you want to Match : dyle

 The First Matching Character = l

Although all the four characters in the second string exist in str1, It returned the output as l. It is because l is the first occurrence letter in string 1 compared to e y d