Syntax
#include <string.h> size_t strcspn(const char *string1, const char *string2);Description
strcspn finds the first occurrence of a byte in string1 that belongs to the set of bytes specified by string2 and calculates the length of the substring pointed to by string1. Ending null characters are not considered in the search.
The strcspn function operates on null-terminated strings. The string arguments to the function should contain a null byte (\0) marking the end of the string.
strcspn returns the index of the first character found. This value is equivalent to the length of the initial substring of string1 that consists entirely of characters not in string2.
This example uses strcspn to find the first occurrence of any of the characters a, x, l or e in string.
#include <stdio.h> #include <string.h> #define SIZE 40 int main(void) { char string[SIZE] = "This is the source string"; char *substring = "axle"; printf("The first %i characters in the string \"%s\" are not in the " "string \"%s\" \n", strcspn(string, substring), string, substring); return 0; /**************************************************************************** The output should be: The first 10 characters in the string "This is the source string" are not in the string "axle" ****************************************************************************/ }Related Information