wcscspn() function in C/C++

The wcscspn() function in C/C++ searches the first occurrence of a wide character of string_2 in the given wide string_1. It returns the number of wide characters before the first occurrence of that wide character . The search includes the terminating null wide characters. Therefore, the function will return the length of string_1 if none of the characters of string_2 are found in string_1.

Syntax:

size_t wcscspn( const wchar_t* string_1, const wchar_t* string_2 )

Parameter : 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 searched

Return value: The function returns two value as below:

  • It returns the number of wide characters in string_2 before the first occurrence of any wide characters present in string_1.
  • length of string_1 is returned if none of the wide characters in string_2 are found in string_1

Below programs illustrate the above function:
Program 1 :



filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate
// wcscspn() function
  
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
    // string to be scanned
    wchar_t string_1[] = L"geeksforgeeks012345";
  
    // string containing rhe character to match
    wchar_t string_2[] = L"0123456789";
  
    // scanning the strings
    int last = wcscspn(string_1, string_2);
  
    // print the position of a matched character
    if (last > wcslen(string_1))
        wcout << string_1 << L" Didn't match any character";
    else
        wcout << L"Occurrence of a character in -> \n"
              << string_1 << " is at position : " << last;
    return 0;
}

chevron_right


Output:

Occurrence of a charcter in ->
 geeksforgeeks012345 is at position : 13

Program 2 :

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate
// wcscspn() function
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
    // string to be scanned
    wchar_t string_1[] = L"GFG";
  
    // string containing rhe character to match
    wchar_t string_2[] = L"909090909";
  
    // scanning the strings
    int last = wcscspn(string_1, string_2);
  
    // print the position of a matched character
    if (last > wcslen(string_1))
        wcout << string_1 << L" does not contain numbers";
    else
        wcout << L"Length of the string -> "
              << string_1 << " is : " << last;
    return 0;
}

chevron_right


Output:

Length of the string -> GFG is : 3


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.