一、strpbrk()函数介绍
在C语言中,其函数原型格式一般为:
char *strpbrk(
const char *str,
const char *strCharSet
);
函数功能:该函数在源字符串(str)中按从前到后顺序找出最先含有搜索字符串(strCharSet)中任一字符的位置并返回位置指针(char*),若找不到则返回空指针NULL。
二、算法实现
char* Stringpbrk(const char* str, const char* strCharSet)
{
while (*str)
{
const char* pSet = strCharSet;
while (*pSet)
if (*pSet++ == *str)
return (char*)str;
++str;
}
return NULL;
}
三、函数示例
函数的调用示例如下面的代码所示:
#include <string.h>
#include <stdio.h>
int main( void )
{
char string[100] = "The 3 men and 2 boys ate 5 pigs\n";
char *result = NULL;
// Return pointer to first digit in "string".
printf( "1: %s\n", string );
result = strpbrk( string, "0123456789" );
//result = Stringpbrk( string, "0123456789" );
printf( "2: %s\n", result++ );
result = strpbrk( result, "0123456789" );
//result = Stringpbrk( result, "0123456789" );
printf( "3: %s\n", result++ );
result = strpbrk( result, "0123456789" );
//result = Stringpbrk( result, "0123456789" );
printf( "4: %s\n", result );
}
输出结果:
1: The 3 men and 2 boys ate 5 pigs
2: 3 men and 2 boys ate 5 pigs
3: 2 boys ate 5 pigs
4: 5 pigs