C Standard Library

C <wchar.h> - wcscspn() Function



The C <wchar.h> strspn() function returns the length of maximum initial segment (span) of the wide string pointed to by ws1 which consists of only the characters not present in the wide string pointed to by ws2.

The search includes the terminating null wide characters and hence returns the length of ws1 when none of the characters of ws2 are found in ws1.

Syntax

size_t wcscspn (const wchar_t* ws1, const wchar_t* ws2);

Parameters

ws1 Specify pointer to the null-terminated wide string to be scanned.
ws2 Specify pointer to the null-terminated wide string containing the characters to match.

Return Value

Returns the length of the initial part of ws1 not containing any of the wide characters present in ws2. Returns the length of ws1 when none of the wide characters of ws2 are found in ws1.

Example:

The example below shows the usage of wcscspn() function.

#include <stdio.h>
#include <wchar.h>
 
int main (){
  wchar_t ws1[100] = L"12345@ABWzx3423S#";
  wchar_t ws2[100] = L"#@,";
  
  //finding the length of maximum initial length of
  //ws1 not containing any wide characters of ws2
  int max_initial_len = wcscspn(ws1, ws2);

  //displaying the result
  printf("Length of maximum initial length: %d",
        max_initial_len);
 
  return 0;
}

The output of the above code will be:

Length of maximum initial length: 5

❮ C <wchar.h> Library

5