The strcspn() function measures how many leading characters in a string belong to a prefix that contains none of the characters in a reject set. It stops at the first “forbidden” character—ideal for finding tokens before delimiters.
01
Prefix Length
size_t result.
02
Reject Set
Stop chars.
03
Not In Set
Complement span.
04
Parse Tokens
Before @ , ;
05
string.h
Standard C.
06
vs strspn
Accept set.
Fundamentals
Definition and Usage
Think of strcspn as complement span: it counts characters from the start of s until it hits any character listed in reject. The return value is that count—not including the stopping character.
If every character in s avoids the reject set, the function returns strlen(s). Pair it with pointer arithmetic to extract substrings without copying: the token is the first len bytes at s.
💡
Beginner Tip
Memory trick: strcspn = complement spn (span). strspn counts chars in the set; strcspn counts chars not in the set.
strspn skips whitespace (chars in the accept set). Then strcspn measures the word (chars not in whitespace). Together they trim and slice input efficiently.
Example 5 — Walk Comma-Separated Tokens
Advance through the string using repeated strcspn calls.
Each iteration prints a token of length len, skips past it, then skips one delimiter byte. For complex parsing, strtok wraps similar logic but modifies the string.
Applications
🚀 Common Use Cases
Delimiter parsing — measure tokens before ,, ;, or |.
Input validation — count leading non-digit characters before a number.
Path segments — length of name before / or \.
Trim + extract — combine with strspn to skip padding.
Custom lexers — scan identifiers until operator symbols.
🧠 How strcspn() Works
1
Start at s[0]
Initialize count to 0.
Scan
2
Check reject set
If current char appears in reject, stop.
Test
3
Increment and continue
Otherwise count++ and move to next char until '\0'.
Count
=
📏
size_t len
Length of prefix with no reject characters.
Important
📝 Notes
Return type is size_t—print with %zu.
Empty reject yields length of entire s.
Time complexity is O(n×m) naive; libraries often optimize with lookup tables.
Does not allocate memory or null-terminate a slice—use %.*s or copy if needed.
For a single delimiter, strchr returns a pointer; strcspn returns a length.
Performance
⚡ Optimization
For a fixed small reject set called many times, a 256-byte bitmap of forbidden characters can beat repeated scans of reject. For occasional parsing, the standard strcspn is clear and fast enough.
Wrap Up
Conclusion
strcspn() returns how many initial characters of s avoid the reject set. Use it to measure tokens before delimiters and combine with strspn() for full parsing pipelines.
Continue with strxfrm() for locale-aware sort keys, or review strspn() for the complementary accept-set span.
Assume the stopping character is included in the length
Print with %s on a slice without length limits
Pass NULL pointers
Forget that reject is a C-string, not a single char only
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about strcspn()
Use these points when parsing strings in C.
5
Core concepts
📏01
Prefix Len
size_t.
Basics
📝02
Reject Set
Stop chars.
Input
🔢03
Not In Set
Complement.
Rule
📈04
Parse
Tokens, CSV.
Use case
💬05
vs strspn
Accept set.
Compare
❓ Frequently Asked Questions
strcspn() returns the length of the longest initial segment of s that contains no characters from the reject set. It stops at the first character in s that appears in reject—or at the end of s if none match.
size_t strcspn(const char* s, const char* reject); Include <string.h>. s is the string to scan. reject is a C-string listing characters to stop on (the delimiter set).
strspn(s, accept) counts leading characters that ARE in accept. strcspn(s, reject) counts leading characters that are NOT in reject. They are complementary: strspn skips allowed chars; strcspn skips until a forbidden char.
It returns strlen(s)—the length of the entire string. Every character belongs to the initial segment with no reject matches.
If reject points to an empty string, strcspn returns the length of s (the whole string), because no character in s can match an empty reject set.
Call len = strcspn(cursor, ",;"). The first len bytes of cursor form a token with no delimiters. Advance with cursor += len, skip the delimiter, and repeat—often paired with strspn to skip separators.
Did you know?
POSIX provides strpbrk(s, accept) to find the pointer to the first character in s that matches any char in accept. strcspn tells you how far you can read before that happens—length vs pointer, both useful in parsers.