Your Browser is not longer supported

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

{{viewport.spaceProperty.prod}}

strcspn - Compare strings and calculate segment length

&pagelevel(4)&pagelevel

Definition

#include <string.h>

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

Starting at the beginning of string s1, strcspn calculates the length of the segment that does not contain a single character from string s2. The terminating null byte (\0) is not treated as part of string s2.

As soon as a character in s1 matches a character in s2, the function is terminated and the segment length is returned.

If the first character in s1 already matches a character in s2, the segment length is equal to 0.

Return val.

Integer

specifying the segment length (number of non-matching 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];
 static char text2[] = "/*#$&";
 size_t n;
 printf("Example of strcspn. Please enter a text line:\n");
 scanf("%s",text1);
 n = strcspn(text1, text2);
 printf("Length of initial segment without /, *, #, $, &: %d\n", n);
 return 0;
}

See also

strspn