Syntax
#include <string.h> size_t strspn(const char *string1, const char *string2);Description
strspn computes the number of bytes in the maximum initial segment of the string pointed to by string1, which consists entirely of bytes from the string pointed to by string2.
strspn returns the index of the first byte found. This value is equal to the length of the initial substring of string1 that consists entirely of bytes from string2. If string1 begins with a byte not in string2, strspn returns 0. If all the bytes in string1 are found in string2, the length of string1 is returned.
This example finds the first occurrence in the array string of a byte that is not an a, b, or c. Because the string in this example is cabbage, strspn returns 5, the length of the segment of cabbage before a byte that is not an a, b, or c.
#include <stdio.h> #include <string.h> int main(void) { char *string = "cabbage"; char *source = "abc"; int index; index = strspn(string, "abc"); printf("The first %d characters of \"%s\" are found in \"%s\"\n", index, string, source); return 0; /**************************************************************************** The output should be: The first 5 characters of "cabbage" are found in "abc" ****************************************************************************/ }Related Information