Case 1 : Enter string 1 : The sun sets in the west. Enter string 2 : aeiou Resultant string : e sun sets in the west. Case 2 : Enter string 1 : LEBANON Enter string 2 : ban Resultant string : NULL Case 3 : Enter string 1 : 2061208 Enter string 2 : 8 Resultant string : 8
Users Online
· Members Online: 0
· Total Members: 188
· Newest Member: meenachowdary055
Forum Threads
Latest Articles
Articles Hierarchy
C++ Program to Implement strpbrk() Function
C++ Program to Implement strpbrk() Function
This is a C++ Program to Implement strpbrk() Function.
The program implements the function strpbrk() which takes two strings and returns a pointer if a string is found in another string.
1. The program takes two strings.
2. Using a for loop, the two strings are compared for same characters.
3. If any character of string 2 matches a character of string 1, string 1 is printed from there.
4. Else NULL is returned.
5. The result is printed.
6. Exit.
Here is the source code of C++ Program to Implement strpbrk() Function. The program output is shown below.
-
#include<iostream>
-
#include<string.h>
-
using namespace std;
-
int main ()
-
{
-
char str1[50], str2[50];
-
int i, j, pos;
-
cout << "Enter string 1 : ";
-
gets(str1);
-
cout << "Enter string 2 : ";
-
gets(str2);
-
cout << "Result : ";
-
for (i = 0 ; str1[i] != '\0'; i++)
-
{
-
for (j = 0; str2[j] != '\0'; j++)
-
{
-
if (str1[i] == str2[j])
-
{
-
pos = j;
-
break;
-
}
-
}
-
}
-
if (pos == 0)
-
{
-
cout << "NULL";
-
return NULL;
-
}
-
while (str1[pos] != '\0')
-
{
-
cout << str1[pos];
-
pos++;
-
}
-
return 0;
-
}
1. The user is asked to enter two strings and stored in ‘str1’ and ‘str2’.
2. Using nested for loops, str1 and str1 are compared.
3. If any part of str2 is present in str1, in a variable ‘pos’ the position is stored and the loop terminates.
4. If pos is equal to 0, means no character matched in both the strings and NULL value is returned.
5. Else str1 is printed from position pos.
6. The result is then printed.