Latest web development tutorials

C library functions - strpbrk ()

C standard library - <string.h> C standard library - <string.h>

description

Character C library functionschar * strpbrk (const char * str1 , const char * str2) to retrieve a string str1stringstr2in the first match of characters does not include the null terminator character. That, in turn determines the string str1 characters when the characters in a string str2 test is also included, stop the test and returns the character position.

statement

Here is () statement strpbrk function.

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

parameter

  • str1 - C strings to be retrieved.
  • str2 - the string contains a list of characters to be matched in the str1.

return value

This function returns the number of characters in the first str1 string str2 the characters in the match, if the character is not found it returns NULL.

Examples

The following example demonstrates strpbrk () function is used.

#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("第一个匹配的字符是: %c\n", *ret);
   }
   else 
   {
      printf("未找到字符");
   }
   
   return(0);
}

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

第一个匹配的字符是: 3

C standard library - <string.h> C standard library - <string.h>