Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

C++ String strpbrk() Function

Posted in C++ Tutorial
Updated on Nov 19, 2024
By Mari Selvan
👁ī¸ 110 - Views
âŗ 4 mins
đŸ’Ŧ 1 Comment
C++ String strpbrk() Function

Photo Credit to CodeToFun

🙋 Introduction

In C++ programming, manipulating and searching strings are common tasks.

The strpbrk() function is a useful string manipulation function that searches for the first occurrence of any character in a specified set within a given string.

In this tutorial, we'll explore the usage and functionality of the strpbrk() function in C++.

💡 Syntax

The signature of the strpbrk() function is as follows:

Syntax
Copied
Copy To Clipboard
const char* strpbrk(const char* str1, const char* str2);

This function takes two C-style strings (str1 and str2) and returns a pointer to the first occurrence in str1 of any character from str2, or a null pointer if no match is found.

📄 Example

Let's delve into an example to illustrate how the strpbrk() function works.

strpbrk.cpp
Copied
Copy To Clipboard
#include <iostream>
#include <cstring>

int main() {
  const char * str1 = "Hello, World!";
  const char * str2 = "aeiou";

  // Find the first occurrence of a vowel in str1
  const char * result = strpbrk(str1, str2);

  // Output the result
  if (result != nullptr) {
    std::cout << "First vowel found: " << * result << std::endl;
  } else {
    std::cout << "No vowel found." << std::endl;
  }

  return 0;
}

đŸ’ģ Output

Output
First vowel found: e

🧠 How the Program Works

In this example, the strpbrk() function searches for the first occurrence of any vowel ('a', 'e', 'i', 'o', or 'u') in the string "Hello, World!" and prints the result.

↩ī¸ Return Value

The strpbrk() function returns a pointer to the first occurrence of any character from the set str2 in the string str1. If no match is found, it returns a null pointer (nullptr).

📚 Common Use Cases

The strpbrk() function is useful when you need to find the first occurrence of any character from a specified set within a string. It simplifies tasks such as searching for specific characters in a text.

📝 Notes

  • The strpbrk() function is part of the C library and can be used with C++.
  • If you need to find the first occurrence of a specific character in a string, consider using functions like std::find from the C++ Standard Library.

đŸŽĸ Optimization

The strpbrk() function is optimized for searching for any character in a set within a string. Consider optimizing your overall algorithm if performance is critical.

🎉 Conclusion

The strpbrk() function in C++ is a valuable tool for searching strings for the first occurrence of any character from a specified set. It provides a convenient way to perform such searches efficiently.

Feel free to experiment with different strings and sets to explore the behavior of the strpbrk() function in various scenarios. Happy coding!

👨‍đŸ’ģ Join our Community:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
👋 Hey, I'm Mari Selvan

For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.

Buy me a coffee to make codetofun.com free for everyone.

Buy me a Coffee

Share Your Findings to All

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Mari Selvan
Mari Selvan
10 months ago

If you have any doubts regarding this article (C++ String strpbrk() Function), please comment here. I will help you immediately.

We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy