C++ Program to Implement strpbrk() Function Last Updated : 27 Nov, 2022 Comments Improve Suggest changes Like Article Like 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; } OutputFirst matching character in str1 is k at position 4Program 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; } OutputFirst 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 checkIf 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; } OutputFirst matching character in str1 is k at position 4 Time Complexity: O(max(n, m)*log(m))Auxiliary Space: O(m) Comment More infoAdvertise with us Next Article C++ Program to Implement strpbrk() Function akashjha2671 Follow Improve Article Tags : Technical Scripter C++ Programs C++ Technical Scripter 2022 STL +1 More Practice Tags : CPPSTL Similar Reads Implementing of strtok() function in C++ The strtok() function is used in tokenizing a string based on a delimiter. It is present in the header file "string.h" and returns a pointer to the next token if present, if the next token is not present it returns NULL. To get all the tokens the idea is to call this function in a loop. Header File: 3 min read C++ Program To Find Simple Interest What is 'Simple Interest'? Simple interest is a quick method of calculating the interest charge on a loan. Simple interest is determined by multiplying the daily interest rate by the principal by the number of days that elapse between payments. Simple Interest formula: Simple interest formula is giv 2 min read C++ Program For String to Long Conversion In this article, we will learn how to convert strings to long in C++. For this conversion, there are 3 ways as follows: Using stol()Using stoul()Using atol() Let's start by discussing each of these methods in detail. Example: Input: s1 = "20" s2 = "30" Output: s1 + s2 long: 50 1. Using stol() In C++ 3 min read C++ Program to Perform Calculations in Pure Strings Given a string of operations containing three operands for each operation "type of command", "first operand", and "second operand". Calculate all the commands given in this string format. In other words, you will be given a pure string that will ask you to perform an operation and you have to perfor 5 min read C++ Program to Create a Temporary File Here, we will see how to create a temporary file using a C++ program. Temporary file in C++ can be created using the tmpfile() method defined in the <cstdio> header file. The temporary file created has a unique auto-generated filename. The file created is opened in binary mode and has access m 2 min read C++ Program For String to Double Conversion There are situations, where we need to convert textual data into numerical values for various calculations. In this article, we will learn how to convert strings to double in C++. Methods to Convert String to Double We can convert String to Double in C++ using the following methods: Using stod() Fun 3 min read C++ Program To Find Compound Interest What is 'Compound interest'? Compound interest is the addition of interest to the principal sum of a loan or deposit, or in other words, interest on interest. It is the result of reinvesting interest, rather than paying it out, so that interest in the next period is then earned on the principal sum 2 min read C++ Program For char to int Conversion In C++, we cannot directly perform numeric operations on characters that represent numeric values. If we attempt to do so, the program will interpret the character's ASCII value instead of the numeric value it represents. We need to convert the character into an integer.Example:Input: '9'Output: 9Ex 2 min read How to Pause a Program in C++? Pausing programs in C ++ is an important common task with many possible causes like debugging, waiting for user input, and providing short delays between multiple operations. In this article, we will learn how to pause a program in C++. Pause Console in a C++ ProgramWe can use the std::cin::get() me 2 min read How to Create a Pointer to a Function in C++? In C++, a function pointer is a variable that stores the address of a function that can later be called through that function pointer. It is useful for passing functions as parameters to other functions(callback functions) or storing them in data structures. In this article, we will learn how to use 2 min read Like