The strpbrk function within the C programming language is a built-in function found in the <string.h> header file. Its primary function is to locate the first occurrence of any character from a specified set of characters in string1 within string2. If no characters from string2 are found in string1, the function returns null.
Syntax of the strpbrk function:
It has the following syntax:
The function char strpbrk(const char string1, const char *string2); is used to locate the first occurrence of any character in string2 in string1 and returns a pointer to that character.
Arguments that are taken by this predefined function:
Here, string1 represents the main string in which you need to search for specific characters. String2 contains the characters that should be present within string1.
Return type of strpbrk function:
The function will provide a pointer that points to the initial occurrence of a character in string1 that is also found in string2. In cases where the character from string2 is not present in string1, the function will return a null pointer, represented as NULL.
Example:
Let's consider a basic program to showcase the functionality of the strpbrk function in the C programming language:
#include <stdio.h>
#include <string.h>
int main() {
char string1[100];
char string2[10];
printf("Enter the string1: ");
scanf("%s", &string1);
printf("Enter the string2: ");
scanf("%s", &string2);
char *result = strpbrk(string1, string2);
if (result != NULL) {
printf("First character from the string2 found at position %ld: %c in string1 \n", result - string1, *result);
} else {
printf("No characters from the string2 found in the string1.\n");
}
return 0;
}
Output:
Enter the string1: Enter the string2: First character from the string2 found at position %ld: %c in string1
No characters from the string2 found in the string1.
Explanation:
In the program mentioned above, the initial step requires the user to input two strings, one for string1 and another for string2. Subsequently, the built-in function is employed, incorporating the <string.h> directive. Should any character from string2 match a character in string1, the function will return the initial instance of that character. Alternatively, if no characters from string2 are found in string1, a message indicating the absence of matching characters is displayed.
Example 2:
Now, let's apply this function in a real-world scenario. For instance, we can verify the strength of a user-provided password by leveraging the strpbrk function in the C programming language.
#include <stdio.h>
#include <string.h>
char* isLowerCase(char* password) {
char alphabet[26] = "abcdefghijklmnopqrstuvwxyz";
char* result = strpbrk(password, alphabet);
return result;
}
char* isUpperCase(char* password) {
char alphabet[26] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char* result = strpbrk(password, alphabet);
return result;
}
char* isContainsSpecialCharacter(char* password) {
char* result = strpbrk(password, "!@#$^&*?");
return result;
}
char* isContainsNumber(char* password) {
char* result = strpbrk(password, "0123456789");
return result;
}
int main() {
char password[100];
printf("Enter the password \n");
scanf("%s", password);
int length = strlen(password);
int flag = 0;
if (length >= 8) {
flag = flag + 1;
} else {
printf("It has less than 8 characters so it is a weak password\n");
}
if (isContainsNumber(password) != NULL) {
flag = flag + 1;
} else {
printf("It does not contain at least one digit or number so it is a weak password\n");
}
if (isLowerCase(password) != NULL) {
flag = flag + 1;
} else {
printf("It does not contain lowercase alphabets so it is a weak password\n");
}
if (isUpperCase(password) != NULL) {
flag = flag + 1;
} else {
printf("It does not contain uppercase alphabets so it is a weak password\n");
}
if (isContainsSpecialCharacter(password) != NULL) {
flag = flag + 1;
} else {
printf("It does not contain special characters so it is a weak password\n");
}
if (flag == 5) {
printf("It is a strong password\n");
}
return 0;
}
Output:
Enter the string1: Enter the string2: First character from the string2 found at position %ld: %c in string1
No characters from the string2 found in the string1.
Explanation:
Within the provided script, the individual is prompted to input their password to evaluate its strength. The password input is saved in a variable named password. This specific variable is then passed on to four distinct functions: isLowerCase, isUpperCase, isContainsSpecialCharacter, and isContainsNumber. The isLowerCase function is responsible for confirming the presence of a minimum of one lowercase character within the password. The isUpperCase function verifies the inclusion of at least one uppercase character within the password. The isContainsSpecialCharacter function ensures the password includes at least one unique character. Lastly, the isContainsNumber function checks for the existence of at least one numerical digit within the password variable. Each function exclusively returns pointers. A password is deemed strong if all pointers are non-null and the password's length is eight characters or more; otherwise, it is considered weak.