C strspn() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
string.h

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.

C
strspn("  hello", " ");
CallWhat it does
strspn(" hello", " ")Counts leading spaces. The result is 2.

Include <string.h>. strcspn() is the opposite idea.

Library declarations
C
size_t strspn(const char *s, const char *accept);

Parameters

ParameterDescription
textRequired. The C-string to scan from the start.
setRequired. The characters that are allowed.

Return Value

Return ValueDescription
LengthHow many leading characters belong to the set.
0Returned 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.

C
#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

  1. Accept only spaces. " " is the allowed set at the start.
  2. Count the span. Two leading spaces → n = 2.
  3. Advance the pointer. s + n skips past those spaces.
  4. 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.

C
#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

  1. Accept digits only. "0123456789" is the set.
  2. Scan from the left. '4' and '2' match; 'a' does not.
  3. Get the count. n = 2 is the digit prefix length.
  4. 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.

C
#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;
}

How It Works

  1. strspn path. Letters at the start are in the alphabet set → count is 5 ("hello").
  2. strcspn path. Characters that are not ',' also run for 5 bytes.
  3. Same length, different idea. One asks “still allowed?”; the other asks “not a delimiter yet?”.
  4. 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.

Next: strcspn()

Count characters until you hit a delimiter from a reject set.

strcspn() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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