Open In App

C++ Program to Implement strpbrk() Function

Last Updated : 27 Nov, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

strpbrk() is a string function in C++ STL that takes in two strings and finds the first occurrence of any character of string2 in string1. This function returns the pointer to the character of string2 in string1 if there is any, otherwise returns NULL.

Syntax

char* strpbrk(const char *str1, const char *str2)

Example:

C++




// C++ code to use strpbrk() function
#include <cstring>
#include <iostream>
using namespace std;
  
int main()
{
    char str1[] = "GeeksforGeeks";
    char str2[] = "strpbrk";
  
    char* pos;
  
    pos = strpbrk(str1, str2);
  
    if (pos != NULL) {
        cout << "First matching character in str1 is "
             << *pos << " at position " << pos - str1 + 1;
    }
  
    else {
        cout << "Character not found" << endl;
    }
  
    return 0;
}


Output

First matching character in str1 is k at position 4

Program to Implement strpbrk() Function In C++

  • Given two character arrays ‘str1’ and ‘str2’.
  • Using nested for loops, str1 and str2 are compared.
  • If any character of str2 is present in str1, then store the ith index of str1 in variable pos and break.
  • If pos is equal to -1, means no character is matched in both the strings so print char is not found.
  • Else print the character and its position in str1.

Example:

C++




// C++ code to implement Implement strpbrk() Function
#include <iostream>
using namespace std;
  
int main()
{
    char str1[20] = "GeeksforGeeks";
    char str2[20] = "strpbrk";
    int pos = -1;
    bool found = 0;
  
    for (int i = 0; str1[i] != '\0'; i++) {
        for (int j = 0; str2[j] != '\0'; j++) {
            if (str1[i] == str2[j]) {
                pos = i;
                found = 1;
                break;
            }
        }
  
        if (found) {
            break;
        }
    }
  
    if (pos != -1) {
        cout << "First matching character in str1 is "
             << str1[pos] << " at position " << pos + 1
             << endl;
    }
    else {
        cout << "Character not found" << endl;
    }
  
    return 0;
}


Output

First matching character in str1 is k at position 4

Time Complexity: O(N2)
Auxiliary Space:  O(1)

Efficient Approach: The given problem can be solved optimally by using a set in C++. Follow the below steps to solve this problem:

  • Given two character arrays ‘str1’ and ‘str2’.
  • Insert all characters of str2 in a set.
  • Traverse str1and check
  • If any character of str2 is present in str1, then store the ith index of str1 in variable pos and break.
  • If pos is equal to -1, means no character is matched in both the strings so print char is not found.
  • Else print the character and its position in str1.

Example:

C++




// C++ Program to implement given problem with Set
#include <iostream>
#include <set>
using namespace std;
  
int main()
{
    char str1[20] = "GeeksforGeeks";
    char str2[20] = "strpbrk";
    int m = sizeof(str2) / sizeof(str2[0]);
    int pos = -1;
  
    set<char> s(str2, str2 + m);
  
    for (int i = 0; str1[i] != '\0'; i++) {
        if (s.find(str1[i]) != s.end()) {
            pos = i;
            break;
        }
    }
  
    if (pos != -1) {
        cout << "First matching character in str1 is "
             << str1[pos] << " at position " << pos + 1
             << endl;
    }
    else {
        cout << "Character not found" << endl;
    }
  
    return 0;
}


Output

First matching character in str1 is k at position 4

Time Complexity: O(max(n, m)*log(m))
Auxiliary Space:  O(m)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads