wcsrchr() function in C/C++

The wcsrchr() function is a builtin function in C/C++ which searches for the last occurrence of a wide character in a wide string. It is defined within the cwchar header file in C++.

Syntax:

wcsrchr(str, ch)

Parameters: The function accepts two parameters which are described below.

  • str: It specifies to the null terminated wide string to be searched for.
  • ch: It specifies wide character to search for.

Return Value: The function returns value of two types:

  • If ch is found, the function returns a pointer to the last location of ch in str.
  • If not found then, a null pointer is returned.

Below programs illustrate the above function.

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate the
// wcsrchr() function
#include <cwchar>
#include <iostream>
using namespace std;
  
int main()
{
    wchar_t str[] = L"GeeksforGeeks";
    wchar_t ch = L'e';
    wchar_t* p = wcsrchr(str, ch);
  
    if (p)
        wcout << L"Last position of " << ch << L" in \""
              << str << "\" is " << (p - str);
    else
        wcout << ch << L" is not present in \"" << str << L"\"";
  
    return 0;
}

chevron_right


Output:

Last position of e in "GeeksforGeeks" is 10

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate the
// wcsrchr() function
#include <cwchar>
#include <iostream>
using namespace std;
  
int main()
{
    wchar_t str[] = L"Ishwar Gupta";
    wchar_t ch = L'o';
    wchar_t* p = wcsrchr(str, ch);
  
    if (p)
        wcout << L"Last position of " << ch << L" in \""
              << str << "\" is " << (p - str);
    else
        wcout << ch << L" is not present in \"" << str << L"\"";
  
    return 0;
}

chevron_right


Output:

o is not present in "Ishwar Gupta"


My Personal Notes arrow_drop_up


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.