C strpbrk() Function

The strpbrk() function in C is a standard library function that locates the first occurrence of any character from a set of characters in a string. It is part of the C standard library (string.h). This function is useful for finding the position of any character from a set of characters within a string.

Table of Contents

  1. Introduction
  2. strpbrk() Function Syntax
  3. Understanding strpbrk() Function
  4. Examples
    • Locating Any Character in a String
    • Using strpbrk() with User Input
  5. Real-World Use Case
  6. Conclusion

Introduction

The strpbrk() function searches for the first occurrence of any character from a specified set of characters in a null-terminated string. It returns a pointer to the character found, or NULL if no such character is found.

strpbrk() Function Syntax

The syntax for the strpbrk() function is as follows:

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

Parameters:

  • str1: A pointer to the null-terminated string to be scanned.
  • str2: A pointer to the null-terminated string containing the characters to match.

Returns:

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

Understanding strpbrk() Function

The strpbrk() function performs a search in str1 for the first occurrence of any character from str2. It returns a pointer to the character in str1 that matches any character in str2, or NULL if no such character is found. The function scans str1 until it finds a match or reaches the null terminator.

Examples

Locating Any Character in a String

To demonstrate how to use strpbrk() to locate any character from a set in a string, we will write a simple program.

Example

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

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

    // Locate the first vowel in the string using strpbrk
    char *result = strpbrk(str1, str2);

    // Print the result
    if (result != NULL) {
        printf("First vowel found at position %ld: '%c'\n", result - str1, *result);
    } else {
        printf("No vowels found\n");
    }

    return 0;
}

Output:

First vowel found at position 1: 'e'

Using strpbrk() with User Input

This example shows how to use strpbrk() to locate any character from a set in a user-provided string.

Example

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

int main() {
    char str1[100];
    char str2[50];

    // Get user input for str1
    printf("Enter the first string: ");
    fgets(str1, sizeof(str1), stdin);
    str1[strcspn(str1, "\n")] = '\0';  // Remove newline character

    // Get user input for str2
    printf("Enter the second string (characters to match): ");
    fgets(str2, sizeof(str2), stdin);
    str2[strcspn(str2, "\n")] = '\0';  // Remove newline character

    // Locate the first occurrence of any character from str2 in str1 using strpbrk
    char *result = strpbrk(str1, str2);

    // Print the result
    if (result != NULL) {
        printf("First matching character found at position %ld: '%c'\n", result - str1, *result);
    } else {
        printf("No matching characters found\n");
    }

    return 0;
}

Output (example user input "Hello, Ramesh!" and target characters "aeiou"):

Enter the first string: Hello, Ramesh!
Enter the second string (characters to match): aeiou
First matching character found at position 1: 'e'

Real-World Use Case

Validating Input Against a Set of Characters

In real-world applications, the strpbrk() function can be used to validate input by checking if it contains any invalid characters.

Example: Validating Input

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

int main() {
    char input[100];
    char invalid_chars[] = "!@#$%^&*()";

    // Get user input
    printf("Enter a string: ");
    fgets(input, sizeof(input), stdin);
    input[strcspn(input, "\n")] = '\0';  // Remove newline character

    // Check for invalid characters using strpbrk
    if (strpbrk(input, invalid_chars) != NULL) {
        printf("Input contains invalid characters\n");
    } else {
        printf("Input is valid\n");
    }

    return 0;
}

Output (example user input "Hello, Ramesh!"):

Enter a string: Hello, Ramesh!
Input contains invalid characters

Conclusion

The strpbrk() function is used for locating the first occurrence of any character from a set in a string in C. By understanding and using this function correctly, you can efficiently search for characters within strings and handle various parsing and validation tasks in your programs. This is particularly helpful in applications that involve input validation, string manipulation, and searching operations.

Comments