Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

strspn - Compare strings and calculate segment length

&pagelevel(4)&pagelevel

Definition

#include <string.h>

size_t strspn(const char *s1, const char *s2);

Starting at the beginning of string s1, strspn calculates the length of the segment that contains only characters from string s2.

As soon as a character in s1 fails to match any character in s2, the function is terminated and the segment length is returned.

If the first character in s1 already fails to match any character in s2, the segment length is equal to 0.

Return val.

Integer value     specifying the segment length (the number of identical characters) 

          starting from the beginning of string s1.

Note

Strings terminated with the null byte (\0) are expected as arguments.

Example

#include <stdio.h>
#include <string.h>
int main(void)
{
 char text1[40];
 char *text2 = "0123456789";
 size_t n;
 printf("Example of strspn. Please enter a text line:\n");
 scanf("%s", text1);
 n = strspn(text1, text2);
 printf("Length of initial segment with digits (0 - 9): %d\n", n);
 return 0;
}

See also

strcspn