- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
strpbrk() in C++
This is a string function in C++ that takes in two strings and finds the first occurrence of any character of string2 in string1. It returns the pointer to the character in string1 if there is any, otherwise returns NULL. This is not applicable for terminating NULL characters.
The syntax of strpbrk() is given as follows −
char *strpbrk(const char *str1, const char *str2)
In the above syntax, strpbrk() returns the pointer to the first character in str1 that matches any character in str2.
A program that demonstrates strpbrk() is given as follows.
Example
#include <iostream> #include <cstring> using namespace std; int main() { char str1[20] = "aeroplane"; char str2[20] = "fun"; char *c; c = strpbrk(str1, str2); if (c != 0) cout<<"First matching character in str1 is "<< *c <<" at position "<< c-str1+1; else printf("Character not found"); return 0; }
Output
First matching character in str1 is n at position 8
In the above program, first the two strings str1 and str2 are defined. The pointer to a character in str1 that is returned by strpbrk() is stored in c. If the value of c is not 0, then the character and its position in str1 is displayed. Otherwise, the character is not there in str1. This is demonstrated by the following code snippet.
char str1[20] = "aeroplane"; char str2[20] = "fun"; char *c; c = strpbrk(str1, str2); if (c != 0) cout<<"First matching character in str1 is "<<*c <<" at position "<< c-str1+1; else printf("Character not found");