Easy Tutorial
❮ C Exercise Example48 C Function Fwrite ❯

C Library Function - strpbrk()

C Standard Library - <string.h>

Description

The C library function char *strpbrk(const char *str1, const char *str2) searches the string str1 for the first occurrence of any character from string str2, excluding the null terminator. That is, it sequentially checks characters in string str1 and stops when it finds a character that is also present in string str2, returning the position of that character.

Declaration

Here is the declaration for the strpbrk() function.

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

Parameters

Return Value

The function returns a pointer to the first matching character in str1 from the characters in str2, or NULL if no such character is found.

Example

The following example demonstrates the use of the strpbrk() function.

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

int main ()
{
   const char str1[] = "abcde2fghi3jk4l";
   const char str2[] = "34";
   char *ret;

   ret = strpbrk(str1, str2);
   if(ret) 
   {
      printf("The first matching character is: %c\n", *ret);
   }
   else 
   {
      printf("No character found");
   }

   return(0);
}

Let's compile and run the above program, which will produce the following result:

The first matching character is: 3

C Standard Library - <string.h>

❮ C Exercise Example48 C Function Fwrite ❯