Home C String Functions strcspn() C strcspn() Function Definition In C, strcspn() counts a leading run of characters that are not in a reject set. It stops at the first space, comma, or other rejected character. strcspn("Hello, world", " ,") is 5.
You pass the text and the reject set. Both are required. The result is a length, and that length is also the index of the first rejected character. Include <string.h>.
Syntax Pass the text, then the reject characters. Start with this call.
strcspn("Hello, world", " ,"); Call What it does strcspn("Hello, world", " ,")Counts letters until the first space or comma. The result is 5.
Include <string.h>. strspn() counts characters that are in the set.
Library declarations size_t strcspn(const char *s, const char *reject); Parameters Parameter Description text Required. The C-string to scan from the start. set Required. The characters that stop the count.
Return Value Return Value Description Length How many characters come before the first rejected character. 0 Returned when the first character is already rejected.
C Examples 1. Until a delimiter 2. Token length 3. No reject match Example 1 — Length Until a Delimiter Find how many characters come before the first comma.
#include <stdio.h>
#include <string.h>
int main(void) {
const char *s = "hello,world";
size_t n = strcspn(s, ",");
printf("length = %zu\n", n);
printf("prefix = \"%.*s\"\n", (int)n, s);
return 0;
} length = 5
prefix = "hello" How It Works Reject set is comma. Any ',' stops the scan.Scan from the start. Letters of "hello" are not in reject.Stop at index 5. The comma is the first reject character.Print the prefix. %.*s prints exactly n characters without needing a temporary buffer.Example 2 — Token Before Spaces or Tabs Treat space and tab as separators and measure the first word.
#include <stdio.h>
#include <string.h>
int main(void) {
const char *line = "Code\ttoFun";
size_t n = strcspn(line, " \t");
printf("token length = %zu\n", n);
printf("token = \"%.*s\"\n", (int)n, line);
return 0;
} token length = 4
token = "Code" How It Works Reject set has whitespace. Space and tab both stop the scan.First four letters count. C o d e are not whitespace.Tab stops the count. n becomes 4 at the tab character.Useful for parsing. Pair with strspn later to skip the separators themselves.Example 3 — No Reject Character Found When nothing in reject appears, the return value equals strlen(s) .
#include <stdio.h>
#include <string.h>
int main(void) {
const char *s = "abcdef";
size_t n = strcspn(s, "xyz");
printf("strcspn = %zu\n", n);
printf("strlen = %zu\n", strlen(s));
return 0;
} How It Works Reject set is unused. None of x y z appear in s.Scan reaches the end. Every character counts toward the prefix.Same as strlen. Both print 6 for "abcdef".Check before slicing. If n == strlen(s), there was no delimiter to split on.Frequently Asked Questions What does strcspn() do in C? strcspn() counts leading characters that are not in the reject set. The length is also the index of the first rejected character.
What are the parameters of strcspn()? Both are required. text is the string to scan, and set lists the stop characters.
What does strcspn() return? A size_t length. 0 means the first character is already rejected.
Which header file does strcspn() need? Include <string.h>.
How is strcspn() different from strspn()? strspn() counts characters that are in the set. strcspn() counts characters that are 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
Helpful Share Copy link Suggestion