strchr和strpbrk之间的区别

时间:2016-10-06 17:45:12

标签: c string stdio

strchr()strpbrk()之间有什么区别?我发现这些之间没有任何区别。

strpbrk()

#include <stdio.h>
#include <string.h>
int main()
{
        char str1[30] = "New Delhi is awesome city", str2[10] = "an";
        char *st;
        st = strpbrk(str1, str2);
        printf("%s"st);
        return 0;
}

输出:awesome city

strchr()

#include <stdio.h>
#include <string.h>
int main()
{
        char str1[] = "New Delhi is awesome city", ch = 'a';
        char *chpos;
        chpos = strchr(str1, ch);
        if(chpos)
                printf("%s",chpos);
        return 0;
}

输出:awesome city

1 个答案:

答案 0 :(得分:2)

文档很清楚。来自strchr()strpbrk()

char *strpbrk(const char *s, const char *accept);

       The strpbrk() function locates the first occurrence in the string s
       of any of the bytes in the string accept.


char *strchr(const char *s, int c);

       The strchr() function returns a pointer to the first occurrence of
       the character c in the string s.

基本上,strpbrk()允许您指定要搜索的多个字符。 在您的示例中,strchr()strpbrk()都会在找到char 'a'后停止,但这并不代表他们做同样的事情!