C library functions -strpbrk()

C Standard Library - <string.h>

Description

C Library Functionschar *strpbrk(const char *str1, const char *str2) Search stringstr1the first matching string instr2The characters in the string, not including the null terminating character. That is, inspect the characters in string str1 sequentially, and when the inspected character is also contained in string str2, stop the inspection and return the position of that character.

Declaration

The following is the declaration of the strpbrk() function.

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

Parameter

  • str1-- The C string to be searched.
  • str2-- This string contains the list of characters to be matched in str1.

Return Value

This function returns the number of characters in str1 before the first matching character from string str2, or returns NULL if no character is found.

Example

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

Example

#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("Character not found"); } return(0); }

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

第一个匹配的字符是: 3

C Standard Library - <string.h>

other extensions