Saturday 11 November 2017

C Program to Implement strpbrk() Function


Code:

#include

char* strpbrk(char *, char *);

int main()
{
    char string1[50], string2[50];
    char *pos;

    printf("Enter the String:\n");
    scanf(" %[^\n]s", string1);
    printf("\nEnter the Character Set:\n");
    scanf(" %[^\n]s", string2);
    pos=strpbrk(string1, string2);
    printf("%s", pos);
}

/* Locates First occurrence in string s1 of any character in string s2, 
 * If a character from string s2 is found , 
 * a pointer to the character in string s1 is returned, 
 * otherwise,  a NULL pointer is returned.
 */
char* strpbrk(char *string1, char *string2)
{
    int i, j, pos, flag = 0;
    for (i = 0; string1[i] != '\0';i++);
    pos = i;
    for (i = 0;string2[i] != '\0';i++)
    {
        for (j = 0;string1[j] != '\0';j++)
        {
            if (string2[i] == string1[j])
            {
                if ( j <= p1)
                {
                    pos = j;    
                    flag = 1;    
                }
            }
         }        
    }
    if (flag == 1)
    {
        return &string1[pos];
    }
    else
    {
        return NULL;
    }
}


Output:

Enter the String:
C programming Class

Enter the Character Set:
mp
programming Class




More C Programs
















100+ Best Home Decoration Ideas For Christmas Day 2019 To Make Home Beautiful

Best gifts for Christmas Day | Greeting cards for Christmas Day | Gift your children a new gift on Christmas day This Christmas d...