CKTIS / C
strcspn — length of a prefix excluding specified characters
strcspn returns the length of an initial string segment containing none of the characters in a second string. The size_t result is the index of the first match if one exists, or strlen(s1) if there is no match.
Version: C23; repository compiler flag, production runtime not attested
Syntax
#include <string.h>
size_t strcspn(const char *s1, const char *s2);strcspn from <string.h> returns, as size_t, the longest prefix length of s1 excluding characters from s2. s2 specifies a character set, not a substring. Neither string is modified.
Without a match, the result is strlen(s1); an empty s2 gives that same result. An empty s1 or an immediate match gives 0. The terminating \0 is not counted.
Both arguments must point to valid \0-terminated strings; NULL is invalid. The result counts bytes, not Unicode characters. In the example, the comma has index 5.
Examples
#include <stdio.h>
#include <string.h>
int main(void)
{
const char text[] = "alpha,beta;gamma";
size_t n = strcspn(text, ",;");
printf("%zu\n", n);
return 0;
}
Result
5
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.