C strspn() Function

Definition
In C, strspn() counts a leading run of characters that belong to an allowed set. It stops at the first outsider. strspn(" Hello", " ") is 3.
You pass the text and the allowed set. Both are required. The result is a length, not a pointer. Include <string.h>.
Syntax
Pass the text, then the allowed characters. Start with this call.
| Call | What it does |
|---|
strspn(" hello", " ") | Counts leading spaces. The result is 2. |
Include <string.h>. strcspn() is the opposite idea.
Library declarations
size_t strspn(const char *s, const char *accept);
Parameters
| Parameter | Description |
|---|
| text | Required. The C-string to scan from the start. |
| set | Required. The characters that are allowed. |
Return Value
| Return Value | Description |
|---|
| Length | How many leading characters belong to the set. |
| 0 | Returned when the first character is not allowed. |
C Examples
Example 1 — Skip Leading Spaces
Use strspn to find how many spaces to skip, then print the rest of the string.
#include <stdio.h>
#include <string.h>
int main(void) {
const char *s = " hello";
size_t n = strspn(s, " ");
printf("span = %zu\n", n);
printf("rest = %s\n", s + n);
return 0;
}
How It Works
- Accept only spaces.
" " is the allowed set at the start. - Count the span. Two leading spaces →
n = 2. - Advance the pointer.
s + n skips past those spaces. - Print the rest. You land on
"hello".
Example 2 — Measure a Digit Prefix
Count how many characters at the start are decimal digits—useful before converting or validating a number string.
#include <stdio.h>
#include <string.h>
int main(void) {
const char *s = "42abc";
size_t n = strspn(s, "0123456789");
printf("digits = %zu\n", n);
printf("after = %s\n", s + n);
return 0;
}
How It Works
- Accept digits only.
"0123456789" is the set. - Scan from the left.
'4' and '2' match; 'a' does not. - Get the count.
n = 2 is the digit prefix length. - See what follows.
s + n points at "abc".
Example 3 — strspn vs strcspn
Same string, two views: strspn counts characters in a set; strcspn counts characters until a reject set.
#include <stdio.h>
#include <string.h>
int main(void) {
const char *s = "hello,world";
printf("strspn letters = %zu\n",
strspn(s, "abcdefghijklmnopqrstuvwxyz"));
printf("strcspn until , = %zu\n",
strcspn(s, ","));
return 0;
}
strspn letters = 5
strcspn until , = 5
How It Works
- strspn path. Letters at the start are in the alphabet set → count is 5 (
"hello"). - strcspn path. Characters that are not
',' also run for 5 bytes. - Same length, different idea. One asks “still allowed?”; the other asks “not a delimiter yet?”.
- Pick by intent. Skip padding with
strspn; measure a token with strcspn.
Frequently Asked Questions
strspn() counts leading characters that belong to an allowed set.
Both are required. text is the string to scan, and set lists the allowed characters.
A size_t length. 0 means the first character is not allowed.
Include <string.h>.
strspn() counts characters in the set. strcspn() counts characters not in the set.
About the author
Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
9 people found this page helpful