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.
/*
* C Program to Implement a strpbrk() Function
*/
#include <stdio.h>
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 <= pos)
{
pos = j;
flag = 1;
}
}
}
}
if (flag == 1)
{
return &string1[pos];
}
else
{
return NULL;
}
}
$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
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.
Related Posts:
- Apply for Computer Science Internship
- Watch Advanced C Programming Videos
- Apply for C Internship
- Check Computer Science Books
- Practice BCA MCQs