C++ Program to Implement strpbrk() Function

This is a C++ Program to Implement strpbrk() Function.

Problem Description

The program implements the function strpbrk() which takes two strings and returns a pointer if a string is found in another string.

Problem Solution

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.

C++ Program/Source code

Here is the source code of C++ Program to Implement strpbrk() Function. The program output is shown below.

  1. #include<iostream>
  2. #include<string.h>
  3. using namespace std;
  4. int main ()
  5. {
  6.     char str1[50], str2[50];
  7.     int i, j, pos;
  8.     cout << "Enter string 1 : ";
  9.     gets(str1);
  10.     cout << "Enter string 2 : ";
  11.     gets(str2);
  12.     cout << "Result : ";
  13.     for (i = 0 ; str1[i] != '\0'; i++)
  14.     {
  15.         for (j = 0; str2[j] != '\0'; j++)
  16.         {
  17.             if (str1[i] == str2[j])
  18.             {
  19.                 pos = j;
  20.                 break;
  21.             }    
  22.         }
  23.     }   
  24.     if (pos == 0)
  25.     {
  26.         cout << "NULL";
  27.         return NULL;
  28.     }
  29.     while (str1[pos] != '\0')
  30.     {
  31.         cout << str1[pos];
  32.         pos++;
  33.     }
  34.     return 0;
  35. }
Program Explanation

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.

advertisement
advertisement
Runtime Test Cases
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

Sanfoundry Global Education & Learning Series – C++ Programs.

To practice all C++ programs, here is complete set of 1000+ C++ Programming examples.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

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.