strcspn() in C

The C library function strcspn() calculates the length of the number of characters before the 1st occurrence of character present in both the string.
Syntax :

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

Parameters:
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 before the 1st occurrence
of character present in both the string.
filter_none

edit
close

play_arrow

link
brightness_4
code

// C code to demonstrate the working of
// strcspn()
#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.
    // returns 3
    size = strcspn(str1, str2);
  
    printf("The unmatched characters before first matched character :  %d\n", size);
}

chevron_right


Output:

The unmatched characters before first matched character :  3

Practical Application : There can be many practical application of this function, be it word games or irregularity calculator. A simple word game is demonstrated in this article.

Rules : According to 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.

filter_none

edit
close

play_arrow

link
brightness_4
code

// C code to demonstrate the application of
// strcspn()
  
#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!! Score : %d", score1);
    else if (score2 > score1)
        printf("Player 2 won!! Score : %d", score2);
    else
        printf("Match Drawn!! Score : %d", score1);
}

chevron_right


Output:

Match Drawn!! Score : 2



My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.