strcspn() in C

Last Updated : 27 May, 2025

The strcspn() is a part of the C standard library, it is defined in the <string.h> header file in C. The strcsspn function is used to find the number of characters in the given string before the 1st occurrence of a character from the defined set of characters or string.

Syntax of strcspn()

C
strcspn(const char *str1, const char *str2)

where,

  • str1 : The Target string in which search has to be made.
  • str2 : Argument string containing characters to match in target string.

Return Value

This function returns the number of characters in the first string before the 1st occurrence
of character present in second string.

Examples of strcspn()

These C programs demonstrate the use of strcspn() for various scenarios.

Using strcspn() to find characters before matching Character

C
#include <stdio.h>
#include <string.h>

int main()
{

    int size;

    // initializing strings
    char str1[] = "geeksforgeeks";
    char str2[] = "kfc";

    // using strcspn() to calculate initial chars
    // before 1st matching chars.
    size = strcspn(str1, str2);
    printf("str1: %s\n",str1);
    printf("str2: %s\n",str2);
    printf("The number of characters in str1 before \nfirst matched character from str2: %d\n", size);
}

Output
str1: geeksforgeeks
str2: kfc
The number of characters in str1 before 
first matched character from str2: 3

Using Strcspn() for a simple word game in C

Rules : In this game, 2 players play and one player initially generated a string and is asked to produce a string which has as many unmatched characters. After 1 round, player producing string with maximum unmatched characters wins.

C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{

    int score1 = 0, score2 = 0, k = 0, sizen = 0, size = 0;

    // initial Round1 strings
    char player1[] = "geeks";
    char play2[] = "";

    while (1) {
        // generating random character
        char randoml = 'a' + (random() % 26);
        play2[k++] = randoml;

        size = strcspn(play2, player1);

        if (size == sizen) {
            // if the character is present, break
            score2 = size;
            break;
        }
        else {
            sizen = size;
        }
    }

    // initial Round2 strings
    const char player2[] = "geeks";
    char play1[] = "";
    k = 0, sizen = 0;

    while (1) {
        // generating random character
        char randoml = 'a' + (random() % 26);
        play1[k++] = randoml;

        size = strcspn(play1, player2);

        if (size == sizen) {

            // if the character is present, break
            score1 = size;
            break;
        }
        else {
            sizen = size;
        }
    }

    if (score1 > score2)
        printf("Player 1 won!! \nScore: %d", score1);
    else if (score2 > score1)
        printf("Player 2 won!! \nScore: %d", score2);
    else
        printf("Match Drawn!! \nScore: %d", score1);
}

Output
Match Drawn!! 
Score: 2

Program for string validation using strcspn()

In this program the strcspn() is used to check if the string is valid, a valid string must not contain any special character like "!@#$%&*".

C
#include <stdio.h>
#include <string.h>

int main()
{

    
    // initializing strings
    char str1[] = "geeksforgeeks";
    char str2[] = "!@#$%&*";
    char str3[] = "geeks@geeks";
    int size1 = sizeof(str1)/sizeof(str1[0]);
    int size3 = sizeof(str3)/sizeof(str3[0]);
    // using strcspn() to calculate initial chars
    // before 1st matching chars.
    printf("String: %s\n",str1);
    if ( strcspn(str1, str2) == size1-1)
    {
        printf("Valid String\n");
    }
    else{
       printf("Invalid String: Contains prohibited special characters\n"); 
    }
    printf("String: %s\n",str3);
    if ( strcspn(str3, str2) == size3-1)
    {
        printf("Valid String");
    }
    else{
       printf("Invalid String: Contains prohibited special characters"); 
    }
    
}

Output
String: geeksforgeeks
Valid String
String: geeks@geeks
Invalid String: Contains prohibited special characters
Comment