C programming string.h function - char *strpbrk(const char *str1, const char *str2)

htt‮p‬s://www.theitroad.com

The strpbrk function in the C programming language, declared in the string.h header file, searches a given string for the first occurrence of any character in a given set of characters.

The function has the following prototype:

char *strpbrk(const char *str1, const char *str2);

Here, str1 is the null-terminated string to be searched, and str2 is the null-terminated string containing the set of characters to search for. The function returns a pointer to the first occurrence of any character from str2 in str1, or a null pointer if no match is found.

Here is an example usage of the strpbrk function:

#include <stdio.h>
#include <string.h>

int main() {
    char str1[] = "This is a test string";
    char str2[] = "ia";
    char *result = strpbrk(str1, str2);

    if (result != NULL) {
        printf("The first character from '%s' found in '%s' is '%c'.\n", str2, str1, *result);
    } else {
        printf("No characters from '%s' were found in '%s'.\n", str2, str1);
    }

    return 0;
}

Output:

The first character from 'ia' found in 'This is a test string' is 'i'.