曲径通幽论坛

标题: strpbrk() -- 从两个字符串中查找到第一个双方相同的字符 [打印本页]

作者: beyes    时间: 2011-12-22 12:45
标题: strpbrk() -- 从两个字符串中查找到第一个双方相同的字符
strpbrk() 函数原型如下:
[Plain Text] 纯文本查看 复制代码
 #include <string.h>
char *strpbrk(const char *s, const char *accept);

该函数用来找到参数 s 和参数 accept 中最先且同时出现的字符,如果找到,那么返回该字符所在的地址,否则返回 NULL 。

测试代码
[C++] 纯文本查看 复制代码
#include <stdio.h>
#include <string.h>

int main()
{
        char *ptr = "www.groad.net";
        char *p;

        p = strpbrk(ptr, "kgb");   /* 'g' 在 s 中最先被找到*/

        printf ("%s\n", p);

        p = strpbrk(ptr, "zyckmfn");   /* 'n' 在 s 中最先被找到*/

        printf ("%s\n", p);

        return 0;
}

运行输出:
./strpbrk
groad.net
net





欢迎光临 曲径通幽论坛 (http://www.groad.net/bbs/) Powered by Discuz! X3.2