w3resource

C strpbrk() function

C strpbrk() function - Scan a string for a byte

Syntax:

char *strpbrk(const char *str, const char *strCharSet)

The strpbrk() function is used to locate the first occurrence in the string pointed to by str of any character from the string pointed to by strCharSet.

Parameters:

Name Description Required /Optional
str Null-terminated, searched string. Required
strCharSet Null-terminated character set. Required

Return value from strpbrk()

  • Upon successful completion, strpbrk() shall return a pointer to the byte.
  • Null pointer if no byte from strCharSet occurs in str.

Example: strpbrk() function


#include <stdio.h>
#include <string.h>
 
int main(void)
{
   char *result, *string = "C Programming";
   char *strCharSet = "mm";
   printf("Original String: C Programming");
   result = strpbrk(string, strCharSet);
   printf("\n\nThe first occurrence of any of the characters \"%s\" in "
          "\"%s\" is \"%s\"\n", strCharSet, string, result);
   char *strCharSet1 = "Pr";
   result = strpbrk(string, strCharSet1);
   printf("\nThe first occurrence of any of the characters \"%s\" in "
          "\"%s\" is \"%s\"\n", strCharSet1, string, result);
   char *strCharSet2 = "Pg";
   result = strpbrk(string, strCharSet2);
   printf("\nThe first occurrence of any of the characters \"%s\" in "
          "\"%s\" is \"%s\"\n", strCharSet2, string, result);
   char *strCharSet3 = "St";
   result = strpbrk(string, strCharSet3);
   printf("\nThe first occurrence of any of the characters \"%s\" in "
          "\"%s\" is \"%s\"\n", strCharSet3, string, result);
} 

Output:

Original String: C Programming

The first occurrence of any of the characters "mm" in "C Programming" is "mming"

The first occurrence of any of the characters "Pr" in "C Programming" is "Programming"

The first occurrence of any of the characters "Pg" in "C Programming" is "Programming"

The first occurrence of any of the characters "St" in "C Programming" is "(null)"

C Programming Code Editor:

Previous C Programming: C strlen()
Next C Programming: C strrchr()



Follow us on Facebook and Twitter for latest update.