C strspn() Function

The strspn() function in C is a standard library function that calculates the length of the initial segment of a string that consists entirely of characters in another specified string. It is part of the C standard library (string.h). This function is useful for finding the length of a segment of a string that contains only characters from a given set.

Table of Contents

  1. Introduction
  2. strspn() Function Syntax
  3. Understanding strspn() Function
  4. Examples
    • Finding Span of Character Set in a String
    • Using strspn() with User Input
  5. Real-World Use Case
  6. Conclusion

Introduction

The strspn() function calculates the length of the initial segment of the string str1 that consists entirely of characters in the string str2. This means it returns the number of characters in str1 that are part of the set of characters in str2.

strspn() Function Syntax

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

size_t strspn(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 set of characters to match.

Returns:

  • The function returns the number of characters in the initial segment of str1 which are part of str2.

Understanding strspn() Function

The strspn() function scans str1 and returns the length of the segment consisting of characters that are all part of str2. The search stops when a character not in str2 is found or when the end of str1 is reached.

Examples

Finding Span of Character Set in a String

To demonstrate how to use strspn() to find the span of a character set in a string, we will write a simple program.

Example

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

int main() {
    char str1[] = "abc123";
    char str2[] = "abc";

    // Find the span using strspn
    size_t span = strspn(str1, str2);

    // Print the result
    printf("The span of characters from '%s' in '%s' is: %zu\n", str2, str1, span);

    return 0;
}

Output:

The span of characters from 'abc' in 'abc123' is: 3

Using strspn() with User Input

This example shows how to use strspn() to find the span of a character set in user-provided input.

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 (set of characters to match): ");
    fgets(str2, sizeof(str2), stdin);
    str2[strcspn(str2, "\n")] = '\0';  // Remove newline character

    // Find the span using strspn
    size_t span = strspn(str1, str2);

    // Print the result
    printf("The span of characters from '%s' in '%s' is: %zu\n", str2, str1, span);

    return 0;
}

Output (example user input "abc123" and character set "abc"):

Enter the first string: abc123
Enter the second string (set of characters to match): abc
The span of characters from 'abc' in 'abc123' is: 3

Real-World Use Case

Validating Input

In real-world applications, the strspn() function can be used to validate input by checking if it consists only of characters from a specified set.

Example: Validating Alphanumeric Input

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

int main() {
    char input[100];
    char valid_chars[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

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

    // Validate input using strspn
    if (strspn(input, valid_chars) == strlen(input)) {
        printf("Input is valid\n");
    } else {
        printf("Input contains invalid characters\n");
    }

    return 0;
}

Output (example user input "Hello123"):

Enter a string: Hello123
Input is valid

Output (example user input "Hello123!"):

Enter a string: Hello123!
Input contains invalid characters

Conclusion

The strspn() function is used for finding the span of characters in a string that belong to a specified set. By understanding and using this function correctly, you can efficiently parse and validate strings in your programs. This is particularly helpful in applications that involve input validation, parsing, and processing text data.

Comments