C语言中的strpbrk

C语言中的strpbrk函数是一个字符串函数,用于查找第一个字符串中与第二个字符串中的任何字符匹配的第一个字符。该语言中strpbrk的语法是

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

或者我们可以简单地将strpbrk写成

strpbrk(str1, str2);

strpbrk C语言示例

此程序将通过多个示例帮助您理解strpbrk。

在使用此strpbrk 字符串函数之前,您必须包含#include<string.h>头文件。

#include <stdio.h> 
#include<string.h>
 
int main()
{
   	char str1[50] = "abcdcefgdhiejk";
   	char str2[50] = "ce";
   	char *result;
	
   	result = strpbrk(str1, str2);
   	
   	if(result)
   	{
   		printf("\n The First Matching Character = %c", *result);	
	}
	else
	{
		printf("\n We haven't found the Character");	
	}
}
strpbrk Example 1

strpbrk示例2

此程序允许用户输入字符串1和字符串2,而不是预定义这两个字符串。接下来,此strpbrk示例程序将查找字符串1中与字符串2中的任何字符匹配的第一个记录。

#include <stdio.h> 
#include<string.h>
 
int main()
{
   	char str1[50], str2[50];
   	char *result;
	
	printf("\n Please Enter any String  : ");
	gets(str1);	
	
	printf("\n Please Enter the String that you want to Match : ");
	gets(str2);	
	
   	result = strpbrk(str1, str2);
   	
   	if(result)
   	{
   		printf("\n The First Matching Character = %c", *result);	
	}
	else
	{
		printf("\n We haven't found the Character");	
	}
}
 Please Enter any String  : tutorial gateway inside

 Please Enter the String that you want to Match : dyle

 The First Matching Character = l

虽然第二个字符串中的所有四个字符都存在于str1中,但输出为l。这是因为与e y d相比,l是字符串1中的第一个出现字母。