6.23.47 wcsstr Function
Locates the first occurrence of a wide string in a wide string.
Include
<wchar.h>
Prototype
wchar_t *wcschr(const wchar_t *s1, const wchar_t * s2);
Arguments
s1
- the wide string in which to search
s2
- the wide string to search for
Return Value
Returns the address of the first element that matches the wide string if found; otherwise, returns a null pointer.
Remarks
This function will find the first occurrence of the wide string s2
(excluding the null terminator) within the string s1
. If s2
points to a zero length string, s1
is returned.
Example
See the notes at the beginning of this chapter or section for
information on using printf()
or scanf()
(and other functions reading and writing the stdin
or
stdout
streams) in the example code.
#include <wchar.h>
int main(void)
{
wchar_t ws1[20] = L"What time is it?";
wchar_t ws2[20] = L"is";
wchar_t ws3[20] = L"xyz";
wchar_t *ptr;
int res;
wprintf(L"ws1 : %ls\n", ws1);
wprintf(L"ws2 : %ls\n", ws2);
wprintf(L"ws3 : %ls\n\n", ws3);
ptr = wcsstr(ws1, ws2);
if (ptr != NULL)
{
res = ptr - ws1 + 1;
wprintf(L"\"%ls\" found at position %d\n", ws2, res);
}
else
wprintf(L"\"%ls\" not found\n", ws2);
wprintf(L"\n");
ptr = wcsstr(ws1, ws3);
if (ptr != NULL)
{
res = ptr - ws1 + 1;
wprintf(L"\"%ls\" found at position %d\n", ws3, res);
}
else
wprintf(L"\"%ls\" not found\n", ws3);
}
Example Output
ws1 : What time is it?
ws2 : is
ws3 : xyz
"is" found at position 11
"xyz" not found