How to use and implement strpbrk in C

The strpbrk function scan the string s1 and finds the first character in the string s1 that matches any character specified in string s2. The search does not include the terminating null-characters of either string but ends there.

Syntax strpbrk in C:

//General Syntax of strpbrk

char *strpbrk(const char *s1, const char *s2);

Parameters:

s1— The null-terminated string to be scanned.

s2— The null-terminated string containing the characters to match.

Return:

The strpbrk function returns a pointer to the character, or a null pointer if no character from s2 occurs in s1.

 

Let’s see an example code to understand the functionality of the strpbrk in C. In this C code, I am trying to find the first vowel in the string s1 finding the character of s1 which locate in s2 with the help of the strpbrk string function.

#include <stdio.h>
#include <string.h>


int main ()
{
    char s1[] = "We Love to read Aticleworld.com";
    char s2[] = "aeiou";

    printf ("Locate first Vowels in '%s'\n",s1);

    //Call strpbrk to locate first char
    //which present in s2
    char *ptr = strpbrk (s1, s2);
    if(ptr != NULL)
    {
        printf ("First Vowel find in s1 is '%c'\n", *ptr);
    }
    else
    {
        printf("There is no Vowels in s1\n");
    }

    return 0;
}

When you run the program, the output will be:

 

strpbrk in c programming

 

Important points you must know before using strpbrk in C:

1. You must include string.h header file before using the strpbrk function in C.

2. The behavior is undefined if either s1 or s2 is not a pointer to a null-terminated byte string.

3. The strpbrk function returns null when no character of s1 found in s2. Let’s see an example code,

#include <stdio.h>
#include <string.h>


int main ()
{
    char s1[] = "We Love to read Aticleworld.com";
    char s2[] = "12345";

    printf ("Locate first Vowels in '%s'\n",s1);

    //Call strpbrk to locate first char
    //which present in s2
    char *ptr = strpbrk (s1, s2);
    if(ptr != NULL)
    {
        printf ("First Vowel find in s2 is '%c'\n", *ptr);
    }
    else
    {
        printf("There is no Vowels in s1\n");
    }

    return 0;
}

When you run the program, the output will be:

strpbrk function in c

 

 

How to implement your own strpbrk function?

Your compiler/standard library will likely have a very efficient and tailored implementation of the strpbrk() function. So if not require avoid to create own version of the strpbrk function.

We can implement the strpbrk function in many ways. Here we are implementing strpbrk using the help of the strchr function. The strchr function returns a pointer to the located character, or a null pointer if the character does not occur in the string. So let’s create our own version of the strpbrk() function in C.

Note: Below function only to understand the working of strpbrk. There is a lot of scenarios that are not handled in this function. You can handle the scenario as your requirement and if possible then use the standard strpbrk function (library function).

char *my_strpbrk(const char *s1, const char *s2)
{
    //return null if any one is NULL
    if((s1 == NULL) || (s2 == NULL))
        return NULL;

    while(*s1)
    {
        //return s1 char position if found in s2
        //if not found return NULL
        if(strchr(s2, *s1))
        {
            //return from function
            //if char found in s2
            return s1;
        }
        else
        {
            s1++;
        }
    }
    return NULL;
}

 

Let’s create a small application to test our own strpbrk() function in C.

#include <stdio.h>
#include <string.h>


char *my_strpbrk(const char *s1, const char *s2)
{
    //return null if any one is NULL
    if((s1 == NULL) || (s2 == NULL))
        return NULL;

    while(*s1)
    {
        //return s1 char position if found in s2
        //if not found return NULL
        if(strchr(s2, *s1))
        {
            //return from function
            //if char found in s2
            return s1;
        }
        else
        {
            s1++;
        }
    }
    return NULL;
}


int main ()
{
    char s1[] = "We Love to read Aticleworld.com";
    char s2[] = "aeiou";

    printf ("Locate first Vowels in '%s'\n",s1);

    //Call my_strpbrk to locate first char
    //which present in s2
    char *ptr = my_strpbrk (s1, s2);
    if(ptr != NULL)
    {
        printf ("First Vowel find in s1 is '%c'\n", *ptr);
    }
    else
    {
        printf("There is no Vowels in s1\n");
    }

    return 0;
}

When you run the program, the output will be:

strpbrk in c programming

 

 

Recommended Articles for you:

Leave a Reply

Your email address will not be published. Required fields are marked *