Javatpoint Logo
Javatpoint Logo

strpbrk() Function in C++

In C++, an enormous number of pre-built functions and libraries are available for handling strings. Strpbrk() is one of the less well-known but very helpful routines. This function is a component of the <cstring> header and is part of the C Standard Library. Its main purpose is to look for a certain set of characters in a string.

  • The main goal of the function strpbrk() is to find the first instance of any character in a given group of characters within a string. If a character in the set is not there, the function produces a null pointer. Otherwise, it returns a pointer to the location of the character detected.
  • The C++ STL string function strpbrk() takes two strings as input and determines where in string1 each character from string2 appears first. If there is a character of string2 in string1, this function returns a pointer to it; if not, it returns NULL.

Syntax:

It has the following syntax:

The parameters are broken out as follows:

  • str: It is the string you want to look for characters.
  • charset: The string that has the characters you want to look for.

Example 1:

Let us take an example to illustrate the strpbrk() function in C++:

Output:

First vowel found at position: 1

Explanation:

  • In this example, the "Hello, World!" is the first string we initialize in the constant character array "text".
  • After that, we define a second constant character array, 'vowels', that has every vowel, capital and lowercase.
  • The 'text' string is searched for the first instance of each character in the 'vowels' string using the strpbrk() function. The 'text' string to find and the 'vowels' string to look for are the two arguments passed to the function.
  • Following the call to strpbrk() function, we examine the search result to see if any matching characters were discovered.
  • If 'strpbrk()' identified a matching character, we subtract the 'text' pointer from the 'result' pointer to determine the character's location inside the 'text' string. It provides us with the index of the 'text' string's first vowel.
  • If the 'strpbrk()' function was unable to locate a corresponding character, we will output a message stating that the string included no vowels.
  • Lastly, we return 0 to signify that the program has run successfully.
  • In this case, strpbrk() provides a straightforward method of locating the first vowel in the given text by streamlining the process of looking for a collection of characters in a string.

Example 2:

Let us take another example to illustrate the strpbrk() function in C++:

Output:

First matching character in str1 is r at position 2

Explanation:

  • In this example, we define 'str1' and 'str2', two character arrays. 'str1' holds the string "Article", and 'str2' holds the string "strpbrk".
  • After that, we define the pointer 'pos', which will hold the strpbrk() function's result. If a matching character is found, this pointer will point to it in 'str1'.
  • We look for the first instance of any character in 'str2' within 'str1' using the strpbrk() method. The 'str1' text to find and the 'str2' string to search for are the two arguments passed to the function.
  • Following the call to strpbrk(), we compare the result to NULL to see if any matching characters were discovered.
  • If the 'strpbrk()' identified a corresponding character, the character (*pos) along with its location within 'str1' are printed. The 1-based position is obtained by subtracting the 'str1' pointer from the 'pos' pointer and adding 1.
  • We print a message stating that the character was not discovered if 'strpbrk()' was unable to locate a match.
  • Lastly, we return 0 to signify that the programme has run successfully.
  • In this case, strpbrk() makes it easier to find the first character from 'str2' in'str1' by giving the character and its location in'str1' in the event that a match is made.

Practical Use Cases:-

There are several use cases of the strpbrk() function in C++. Some main use cases of the strpbrk() function in C++ are as follows:

1. Tokenization:-

  • The practice of breaking a string up into smaller parts, or tokens, is called Tokenization.
  • You can tokenize a string by specifying a set of delimiters using strpbrk().
  • For example, you can use punctuation and spaces as delimiters to break a statement into words.

2. Input Validation:-

  • Use strpbrk() to see if user input contains any characters that you don't want it to when you expect certain characters from them.
  • It is helpful for regulated and secure data validation.

3. Extracting Substrings:-

  • Use the strpbrk() function to find particular substrings inside a longer string.
  • For example, you can use strpbrk() to find word boundaries if you want to extract all words from a text that contains the letter "a".

4. Custom String Manipulation:-

  • A useful tool in more intricate string manipulation activities is strpbrk().
  • For example, you can perform custom transformations on strings by combining them with other methods.






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA