V Abinaya
V Abinaya

Reputation: 85

Difference between strchr and strpbrk

What is the difference between strchr() and strpbrk(). I couldn't find any difference between those.

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;
}

output: 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;
}

output: awesome city

Upvotes: 4

Views: 2580

Answers (1)

P.P
P.P

Reputation: 121387

The documentation is clear. From strchr() and 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.

Basically, strpbrk() allows you to specify multiple chars to be searched. In your example, both strchr() and strpbrk() both stop after finding the char 'a' but that doesn't mean they do the same thing!

Upvotes: 4

Related Questions