The wcsrchr
function is used to locate the last occurrence of a wide character in a wide string. It is defined in the wchar.h
header.
wchar_t* wcsrchr (wchar_t* str, wchar_t ch);
The function takes two parameters of type wchar_t
.
In the above snippet of code, ch
is a wide character that will be searched in the wide string pointed by str
.
If ch
is found in str
, the function returns a pointer to its last occurrence. If ch
is not found, the function returns NULL
.
#include<stdio.h>#include<wchar.h>void main() {// declaring and initializing wide stringwchar_t str1[] = L"HELLO WORLD.";// calling the functionwchar_t* ptr = wcsrchr(str1, L'O');if(ptr == NULL) {wprintf(L"Character not found! \n");}else {// using the wprintf function to print the wide stringwprintf(L"RESULT: \n");wprintf(L"ptr = %ls\n\n", ptr);}}
In the above snippet of code, we’re searching the last occurrence of ‘O’ in str1
.
We can see from the output of the code that the function returned the last occurrence of ‘O’.
Free Resources