Easy Tutorial
❮ C Function Strlen C Standard Library Stdio H ❯

C Library Function - strcspn()

C Standard Library - <string.h>

Description

The C library function size_t strcspn(const char *str1, const char *str2) retrieves the number of initial consecutive characters in the string str1 that are not in the string str2.

Declaration

Here is the declaration for the strcspn() function.

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

Parameters

Return Value

The function returns the number of characters at the beginning of str1 that do not contain any characters from str2.

Example

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

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

int main ()
{
   int len;
   const char str1[] = "ABCDEF4960910";
   const char str2[] = "013";

   len = strcspn(str1, str2);

   printf("The first matching character is at %d\n", len + 1);

   return(0);
}

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

The first matching character is at 10

C Standard Library - <string.h>

❮ C Function Strlen C Standard Library Stdio H ❯