Easy Tutorial
❮ C Function Clock C Return Pointer From Functions ❯

C Library Function - strspn()

C Standard Library - <string.h>

Description

The C library function size_t strspn(const char *str1, const char *str2) retrieves the length of the initial segment of str1 which consists entirely of characters that are part of str2.

Declaration

Here is the declaration for the strspn() function.

size_t strspn(const char *str1, const char *str2)

Parameters

Return Value

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

Example

The following example demonstrates the use of the strspn() function.

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

int main ()
{
   int len;
   const char str1[] = "ABCDEFG019874";
   const char str2[] = "ABCD";

   len = strspn(str1, str2);

   printf("Length of initial segment matching %d\n", len );

   return(0);
}

Let's compile and run the above program, this will produce the following result:

Length of initial segment matching 4

C Standard Library - <string.h>

❮ C Function Clock C Return Pointer From Functions ❯