strcspn() function in C/C++

The strcspn() function in C/C++ takes two string as input, string_1 and string_2 as it’s argument and searches string_1 by traversing for any characters that is present in string_2 . The function will return the length of string_1 if none of the characters of string_2 are found in string_1 . This function is defined in cstring header file .

Syntax :

size_t strcspn ( const char *string_1, const char *string_2 ) 

Parameters : The function accepts two mandatory parameters which are described below:

  • string_1: specifies the string to be searched
  • string_2: specifies the string containing the characters to be matched

Return Value : This function returns the number of character traversed in string_1 before any of the character matched from string_2 .

Below programs illustrate the above function:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate the
// strcspn() function
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
    // String to be travered
    char string_1[] = "geekforgeeks456";
  
    // Search these characters in string_1
    char string_2[] = "123456789";
  
    // function strcspn() traverse the string_1
    // and search the characters of string_2
    size_t match = strcspn(string_1, string_2);
  
    // if matched return the position number
    if (match < strlen(string_1))
        cout << "The number of characters before"
             << "the matched character are " << match;
  
    else
        cout << string_1 << 
        " didn't matched any character from string_2 ";
    return 0;
}

chevron_right


Output:

The number of characters beforethe matched character are 12

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate the
// strcspn() function When the string
// containing the character to be
// matched is empty
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
    // String to be travered
    char string_1[] = "geekforgeeks456";
  
    // Search these characters in string_1
    char string_2[] = ""; // Empty
  
    // function strcspn() traverse the string_1
    // and search the characters of string_2
    size_t match = strcspn(string_1, string_2);
  
    // if matched return the position number
    if (match < strlen(string_1))
        cout << "The number of character before"
             << "the matched character are " << match;
    else
        cout << string_1 << 
        " didn't matched any character from string_2  ";
    return 0;
}

chevron_right


Output:

geekforgeeks456 didn't matched any character from string_2


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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 :


Be the First to upvote.


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