C++ strspn() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
<cstring>

Definition

In C++, strspn() looks only at the start of a C-string and counts how many characters in a row belong to an allowed set. It stops at the first character that is not in that set.

Both arguments are required. The result is a length, not a pointer. It is 0 when the first character is not allowed. strcspn() is the opposite: it counts characters that are not in a reject set. Include <cstring>.

Syntax

The second string lists the characters that are allowed.

C++
strspn("   Hello", " ");
CallWhat it does
strspn(" Hello", " ")Counts the spaces at the start. The result is 3.
Library declarations
C++
std::size_t strspn(const char* dest, const char* src);

Parameters

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

Return Value

Return ValueDescription
LengthHow many starting characters are in the allowed set.
0The first character is not in the set.

C++ Examples

Example 1 — Basic strspn()

Count leading lowercase letters.

C++
#include <iostream>
#include <cstring>

int main() {
    const char* text = "abcde312$#@";
    const char* letters = "abcdefghijklmnopqrstuvwxyz";

    std::size_t n = std::strspn(text, letters);
    std::cout << "Accepted length: " << n << '\n';
    std::cout << "Remainder: " << (text + n) << '\n';
    return 0;
}

How It Works

  1. Scans from the start while each character is a lowercase letter.
  2. Stops at '3', which is not in the accept set.
  3. text + n points at the remainder.

Example 2 — Skip Leading Spaces

Jump past whitespace before reading a word.

C++
#include <iostream>
#include <cstring>

int main() {
    const char* line = "   hello";
    std::size_t spaces = std::strspn(line, " \t");

    std::cout << "Spaces skipped: " << spaces << '\n';
    std::cout << "Word: " << (line + spaces) << '\n';
    return 0;
}

How It Works

  1. Accept set is space and tab.
  2. Three spaces match at the start.
  3. Parsing continues at line + spaces.

Example 3 — Zero Length

If the first character is not accepted, the result is 0.

C++
#include <iostream>
#include <cstring>

int main() {
    const char* text = "42apples";
    std::size_t n = std::strspn(text, "abcdefghijklmnopqrstuvwxyz");

    std::cout << "Accepted length: " << n << '\n';
    return 0;
}

How It Works

  1. First character is '4', not a letter.
  2. The span stops immediately.
  3. Return value is 0.

Frequently Asked Questions

strspn() counts how many characters at the start of a C-string belong to an allowed set.
text is the C-string to scan. set is the characters that are allowed.
The number of allowed characters at the start. It returns 0 when the first character is not allowed.
strspn() counts characters that are in the set. strcspn() counts characters that are not in the set.
No. It stops at the first character that is not in the allowed set.

Back to String Functions

Browse the full index of C++ string and <cstring> tutorials.

Functions index →

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.

8 people found this page helpful