C Program to Implement strpbrk() Function

This C Program implements strpbrk() function.

Here is source code of the C Program to implement strpbrk() function. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to Implement a strpbrk() Function
  3.  */
  4. #include <stdio.h>
  5.  
  6. char* strpbrk(char *, char *);
  7.  
  8. int main()
  9. {
  10.     char string1[50], string2[50];
  11.     char *pos;
  12.  
  13.     printf("Enter the String:\n");
  14.     scanf(" %[^\n]s", string1);
  15.     printf("\nEnter the Character Set:\n");
  16.     scanf(" %[^\n]s", string2);
  17.     pos=strpbrk(string1, string2);
  18.     printf("%s", pos);
  19. }
  20.  
  21. /* Locates First occurrence in string s1 of any character in string s2, 
  22.  * If a character from string s2 is found , 
  23.  * a pointer to the character in string s1 is returned, 
  24.  * otherwise,  a NULL pointer is returned.
  25.  */
  26. char* strpbrk(char *string1, char *string2)
  27. {
  28.     int i, j, pos, flag = 0;
  29.     for (i = 0; string1[i] != '\0';i++);
  30.     pos = i;
  31.     for (i = 0;string2[i] != '\0';i++)
  32.     {
  33.         for (j = 0;string1[j] != '\0';j++)
  34.         {
  35.             if (string2[i] == string1[j])
  36.             {
  37.                 if (j <= pos)
  38.                 {
  39.                     pos = j;    
  40.                     flag = 1;    
  41.                 }
  42.             }
  43.          }        
  44.     }
  45.     if (flag == 1)
  46.     {
  47.         return &string1[pos];
  48.     }
  49.     else
  50.     {
  51.         return NULL;
  52.     }
  53. }

$gcc string34.c
$ a.out
Enter the String:
C programming Class
 
Enter the Character Set:
mp
programming Class

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement
advertisement

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

If you wish to look at programming examples on all topics, go to C Programming Examples.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.