首页 > 编程笔记 > C++笔记 阅读:18

C++ strpbrk()函数的用法(附带实例)

strpbrk() 是 C 语言标准库中的函数,位于 <string.h> 头文件中。

strpbrk() 函数可检索两个字符串中首个相同字符的位置(不检索字符串结束符 '\0')。函数原型如下:
char * strpbrk(const char * s1, const char * s2) ;
其中,s1、s2 为待检索的两个字符串。如果 s1、s2 含有相同的字符,那么返回指向 s1 中第一个相同字符的指针,否则返回 NULL。

【实例】使用 strpbrk() 函数比较两个字符串中是否包含相同字符。
#include <stdio.h>
#include <string.h>
int main()
{
    char* s1 = "If winter comes,can spring be far behind ?";
    char* s2 = "winter";
    char* p = strpbrk(s1,s2);
    if(p){
        printf("The result is: %s\n",p);
    }else{
        printf("Not Found!\n");
    }
    return 0;
}
程序运行结果为:

The result is: winter comes,can spring be far behind ?

相关文章