Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

C strpbrk() Function

Posted in C Tutorial
Updated on Oct 06, 2024
By Mari Selvan
👁ī¸ 178 - Views
âŗ 4 mins
đŸ’Ŧ 1 Comment
C strpbrk() Function

Photo Credit to CodeToFun

🙋 Introduction

In C programming, dealing with strings is a fundamental aspect, and various functions are available for string manipulation.

The strpbrk() function is one such function that is used to find the first occurrence of any character in a set of specified characters within a given string.

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

💡 Syntax

The strpbrk() function has the following signature:

Syntax
Copied
Copy To Clipboard
char *strpbrk(const char *str1, const char *str2);
  • str1: The null-terminated string to be searched.
  • str2: The null-terminated string containing characters to search for.

📄 Example

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

strpbrk.c
Copied
Copy To Clipboard
#include <stdio.h>
#include <string.h>

int main() {
  const char str1[] = "Hello, C Programming!";
  const char str2[] = "aeiou";

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

  if (result != NULL) {
    printf("First vowel found: %c\n", * result);
  } else {
    printf("No vowel found.\n");
  }

  return 0;
}

đŸ’ģ Output

Output
First vowel found: e

🧠 How the Program Works

In this example, the strpbrk() function is used to find the first occurrence of any vowel (a, e, i, o, u) in the string "Hello, C Programming!".

↩ī¸ Return Value

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

📚 Common Use Cases

The strpbrk() function is particularly useful when you need to search for any character from a specified set within a string. It is commonly employed in scenarios where you want to identify the presence of certain characters in a larger text.

📝 Notes

  • The search is case-sensitive. If a case-insensitive search is required, consider converting the strings to lowercase (or uppercase) using functions like tolower() or toupper() before calling strpbrk().
  • The function is part of the C standard library and is declared in the <string.h> header.

đŸŽĸ Optimization

The strpbrk() function is optimized for efficiency, and no specific optimizations are typically needed.

🎉 Conclusion

The strpbrk() function in C is a valuable tool for searching for the first occurrence of any character from a specified set within a string. It provides flexibility in identifying specific characters and is commonly used in text processing tasks.

Feel free to experiment with different strings and character sets to deepen your understanding of the strpbrk() function. 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
11 months ago

If you have any doubts regarding this article (C 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