header. The main purpose of this function is to get the first occurrence of the character present in the set of characters of string2 in string1.">

strpbrk() function in C

In this article, you will learn about the strpbrk() function in C with its syntax and examples.

What is the strpbrk() function?

The strpbrk() function in C language is a standard library predefined function in the <string.h> header. The main purpose of this function is to get the first occurrence of the character present in the set of characters of string2 in string1. It returns null if no character in the string2 is present in the string1

Syntax of the strpbrk() function:

It has the following syntax:

char *strpbrk (const char *string1, const char *string2);

Arguments that are taken by this predefined function:

Here string1 is the string where you have to search for the character. String2 is the string of characters that are wanted to be present in string1

Return type of strpbrk() function:

The function will return a pointer pointing to the first occurrence of the character in string1, which is also present in the character in string2. If the character in string2 is not present in the string1, this function returns the null pointer that is NULL.

Example:

Let us take a simple program for demonstrating the strpbrk() function in C:

Output:

strpbrk() function in C

Explanation:

In the above program, initially, the user has to give two strings as inputs for both string1 and string2. After that, we directly use the predefined function, which includes the <string.h> header. If any character present in the strig2 is also present in the string1, it returns the first occurrence of the character. Otherwise, it gives a message like no characters from the string2 found in string1.

Example 2:

Now, let's use this function for practical purposes. Like checking whether the password given by the user is strong or not by using the strpbrk() function in C.

Output:

strpbrk() function in C

Explanation:

In the above program, the user has to enter his or her password to check whether the entered password is strong or weak. The user input is stored in the password variable. This variable is sent to the four functions that are isLowerCase, isUpperCase, isContainsSpecialCharacter, and isContainsNumber. The isLowerCase function will check whether the password will contain at least one lowercase character. The isUpperCase function will check whether the password will contain at least one uppercase character in the password. The isContainsSpecialCharacter function will check whether the password contains at least one unique character. The isContainsNumber function will check whether it has at least one digit or not in the password variable. All the functions will return pointers only. If all the pointers are not null and the length of the password is greater than or equal to 8, this password is firm, or it is a weak password.