CKTIS / C
strspn — length of a prefix containing allowed characters
strspn returns the length of the initial portion of a string containing only characters from a supplied set.
Version: C23; repository compiler flag, production runtime not attested
Syntax
#include <string.h>
size_t strspn(const char *s1, const char *s2);strspn, declared in <string.h>, returns as size_t the length of the longest prefix of s1 containing only characters found in s2. s2 specifies a character set, not a substring to match; order and duplicates do not matter.
Counting stops before the first disallowed character or terminating \0, which is not counted. Empty s1 or s2 gives 0. Neither string is modified. Both arguments must point to valid null-terminated strings; NULL is invalid. The result counts bytes, not Unicode characters.
In the example, n is 4: counting stops before x. text + n points to the remaining x45; %zu prints size_t.
Examples
#include <stdio.h>
#include <string.h>
int main(void)
{
const char text[] = "1203x45";
size_t n = strspn(text, "0123456789");
printf("%zu\n", n);
printf("%s\n", text + n);
return 0;
}Result
4 x45
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.