wcsspn
in C returns the length of the maximum initial portion of a wide string whose characters are present in another wide string. wcsspn
is defined in the wchar.h
header and its function declaration in the standard C library is as follows:
size_t wcsspn(const wchar_t* dest, const wchar_t* src);
A wide string uses characters from the Unicode character set, where typically each character is of 2 bytes
dest
: the wide string that is to be analyzed
src
: the wide string that contains the characters to search for
Maximum initial portion of dest
whose characters are present in src
.
In C++,
wcsspn
is defined in thecwchar
header.
#include <wchar.h>int main (){int length1, length2, length3;wchar_t src1[] = L"2021 is going to be my year";wchar_t src2[] = L"20-20 is the best format for cricket";wchar_t src3[] = L"Edpresso-a shot of dev knowledge";wchar_t dest[] = L"2021Edpresso";length1 = wcsspn(dest,src1);length2 = wcsspn(dest,src2);length3 = wcsspn(dest,src3);wprintf(L"Example 1. Length of initial matching characters: %d\n",length1);wprintf(L"Example 2. Length of initial matching characters: %d\n",length2);wprintf(L"Example 3. Length of initial matching characters: %d\n",length3);return 0;}
First, we import the wchar.h
header file. In lines 7-11, we initialize the destination and source strings, where the L identifier before the string indicates that the characters to follow are from the Unicode character set.
dest
is 2021.dest
is 202.dest
. Although Edpresso
is part of both wide strings, dest
starts with 2 and is not part of the initial portion of dest
.Free Resources