C strcspn() Function

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

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.

C
strcspn("Hello, world", " ,");
CallWhat 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
C
size_t strcspn(const char *s, const char *reject);

Parameters

ParameterDescription
textRequired. The C-string to scan from the start.
setRequired. The characters that stop the count.

Return Value

Return ValueDescription
LengthHow many characters come before the first rejected character.
0Returned when the first character is already rejected.

C Examples

Example 1 — Length Until a Delimiter

Find how many characters come before the first comma.

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

How It Works

  1. Reject set is comma. Any ',' stops the scan.
  2. Scan from the start. Letters of "hello" are not in reject.
  3. Stop at index 5. The comma is the first reject character.
  4. 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.

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

How It Works

  1. Reject set has whitespace. Space and tab both stop the scan.
  2. First four letters count. C o d e are not whitespace.
  3. Tab stops the count. n becomes 4 at the tab character.
  4. 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).

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

  1. Reject set is unused. None of x y z appear in s.
  2. Scan reaches the end. Every character counts toward the prefix.
  3. Same as strlen. Both print 6 for "abcdef".
  4. Check before slicing. If n == strlen(s), there was no delimiter to split on.

Frequently Asked Questions

strcspn() counts leading characters that are not in the reject set. The length is also the index of the first rejected character.
Both are required. text is the string to scan, and set lists the stop characters.
A size_t length. 0 means the first character is already rejected.
Include <string.h>.
strspn() counts characters that are in the set. strcspn() counts characters that are not in the set.

Next: strxfrm()

Transform a string for locale-aware comparison and sorting.

strxfrm() 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