Home C++ Strings Functions strspn() C++ strspn() Function 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.
Call What it does strspn(" Hello", " ")Counts the spaces at the start. The result is 3.
Library declarations std::size_t strspn(const char* dest, const char* src); 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 starting characters are in the allowed set. 0 The first character is not in the set.
C++ Examples 1. Basic span 2. Skip spaces 3. Zero length Example 1 — Basic strspn() Count leading lowercase letters.
#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;
} Accepted length: 5
Remainder: 312$#@ How It Works Scans from the start while each character is a lowercase letter. Stops at '3', which is not in the accept set. text + n points at the remainder.Example 2 — Skip Leading Spaces Jump past whitespace before reading a word.
#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;
} Spaces skipped: 3
Word: hello How It Works Accept set is space and tab. Three spaces match at the start. Parsing continues at line + spaces. Example 3 — Zero Length If the first character is not accepted, the result is 0.
#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 First character is '4', not a letter. The span stops immediately. Return value is 0. Frequently Asked Questions What does strspn() do in C++? strspn() counts how many characters at the start of a C-string belong to an allowed set.
What are the parameters of strspn()? text is the C-string to scan. set is the characters that are allowed.
What does strspn() return? The number of allowed characters at the start. It returns 0 when the first character is not allowed.
How is strspn() different from strcspn()? strspn() counts characters that are in the set. strcspn() counts characters that are not in the set.
Does strspn() skip the whole string? 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 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.
8 people found this page helpful
Helpful Share Copy link Suggestion