strspn()

Updated: April 19, 2023

Count the characters at the beginning of a string that are in a given character set

Synopsis:

#include <string.h>

size_t strspn( const char* str,
               const char* charset );

Arguments:

str
The string that you want to search.
charset
The set of characters you want to look for.

Library:

libc

Use the -l c option to qcc to link against this library. This library is usually included automatically.

Description:

The strspn() function computes the length of the initial segment of the string pointed to by str that consists of characters from the string pointed to by charset. The terminating null character isn't considered to be part of charset.

Returns:

The length of the segment.

Examples:

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

int main( void )
  {
    printf( "%d\n", strspn( "out to lunch", "aeiou" ) );
    printf( "%d\n", strspn( "out to lunch", "xyz" ) );
    return EXIT_SUCCESS;
  }

produces the output:

2
0

Classification:

ANSI, POSIX 1003.1

Safety:  
Cancellation point No
Interrupt handler Yes
Signal handler Yes
Thread Yes