Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
strpbrk()
2.1.
Syntax
2.2.
Output
3.
FAQs
3.1.
What is C language?
3.2.
What does strpbrk() do in C?
3.3.
What does strpbrk() return?
3.4.
How do we import strpbrk() into our code in C?
3.5.
Why do we use string functions in C?
4.
Key Takeaways
Last Updated: Mar 27, 2024
Easy

strpbrk() in C

Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

C is a procedure-oriented programming language created by Dennis Ritchie in 1972. C language provides us with multiple string functions that can save us from writing many lines of code. We can use the default functions instead of the long codes to get our work done. The string functions are included in the “string.h” header file. In this article, let’s learn about a string function - strpbrk().

You can also read about the dynamic arrays in c and C Static Function

strpbrk()

The strpbrk() function is available in the string.h header file is used to find the first character of the first string, which matches the characters in the second string. In other words, the strpbrk() function finds the first character, which is present in both the strings. Let’s learn how to use strpbrk() with an example.

Syntax

char *result = strpbrk(string1, string2); 

#include<stdio.h>
#include<string.h>
void main () {
   char str1[] = "codingninjas";
   char str2[] = "code";
   char *result;
   result = strpbrk(str1, str2);
   printf("The first matching character in both strings is: %c\n", *result);
}

Output

The first matching character in both strings is: c

We included the header files stdio.h for handling input, output, and string.h for handling the string functions in the above code. We initialized two strings, str1[] and str2 are character arrays because C doesn't support the declaration of strings directly. The strpbrk() function searches for the first character present in str1 and str2 and prints the above output.

We initialized the two strings in the above code, but we can also allow the users to give any input of their choice and execute this function on them. 

Also read,  Tribonacci Series and Short int in C Programming

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

FAQs

What is C language?

C is a high-level and procedure-oriented programming language ideal for developing firmware applications. It was created by Dennis Ritchie in 1972.

What does strpbrk() do in C?

The strpbrk() function finds the first character of the first string, which matches with the characters in the second string. It is available in the string.h header file.

What does strpbrk() return?

The strpbrk() function returns the first matching character of the “char *” data type in two strings. If no matching character is found, it returns NULL.

How do we import strpbrk() into our code in C?

We import the libraries by including header files in the C programming language. We include the string.h header file in the code to use the strpbrk() string function. 

Why do we use string functions in C?

The string functions in C are easy to use, and they can replace lengthy codes with a single function. Long lines of code might increase the complexity of our code, and it can take a lot of time to execute. So we use string functions to overcome this problem.

Key Takeaways

We have discussed the concept of the strpbrk() in C in this article with clear examples and a code. You can now use the functions in your code. 

Check out this problem - Multiply Strings

Hey Ninjas! We hope this blog helped you better understand the strpbrk() concept in C. Please check out Coding Ninjas for more unique courses and guided paths. Also, try Coding Ninjas Studio for more exciting articles, interview experiences, and fantastic Data Structures and Algorithms problems. Please upvote our blog to help the other ninjas grow.

Happy Coding!

Previous article
String compare in C
Next article
ispunct() in C
Live masterclass