Introduction
C is a procedure-oriented programming language created by Dennis Ritchie in 1972. C language provides us with multiple string functions that can save us from writing many lines of code. We can use the default functions instead of the long codes to get our work done. The string functions are included in the “string.h” header file. In this article, let’s learn about a string function - strpbrk().
You can also read about the dynamic arrays in c and C Static Function
strpbrk()
The strpbrk() function is available in the string.h header file is used to find the first character of the first string, which matches the characters in the second string. In other words, the strpbrk() function finds the first character, which is present in both the strings. Let’s learn how to use strpbrk() with an example.
Syntax
char *result = strpbrk(string1, string2);
#include<stdio.h>
#include<string.h>
void main () {
char str1[] = "codingninjas";
char str2[] = "code";
char *result;
result = strpbrk(str1, str2);
printf("The first matching character in both strings is: %c\n", *result);
}Output
The first matching character in both strings is: c
We included the header files stdio.h for handling input, output, and string.h for handling the string functions in the above code. We initialized two strings, str1[] and str2 are character arrays because C doesn't support the declaration of strings directly. The strpbrk() function searches for the first character present in str1 and str2 and prints the above output.
We initialized the two strings in the above code, but we can also allow the users to give any input of their choice and execute this function on them.
Also read, Tribonacci Series and Short int in C Programming




