C++ Standard Library C++ STL Library

C++ <cwchar> - wmemchr() Function



The C++ <cwchar> wmemchr() function is used to find the first occurrence of wc in first num wide characters of the wide character array pointed to by ptr.

The function returns pointer to the first occurrence of wc in ptr, or null wide pointer if wc is not found in ptr.

Syntax

const wchar_t* wmemchr (const wchar_t* ptr, wchar_t wc, size_t num);
wchar_t* wmemchr ( wchar_t* ptr, wchar_t wc, size_t num);

Parameters

ptr Specify pointer to the wide character array where the search is performed.
wc Specify the wide character to search for.
num Specify the maximum number of wide characters to be examined.
size_t is an unsigned integer type.

Return Value

Returns pointer to the first occurrence of ch in ptr, or null wide pointer if ch is not found in ptr.

Example:

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

#include <iostream>
#include <cwchar>
using namespace std;
 
int main (){
  wchar_t MyStr[100] = L"To be, or not to be, that is the question.";
  wchar_t * search;
  
  //searching for first occurrence of 'e' 
  //in first 25 wide characters of MyStr
  search = (wchar_t*) wmemchr(MyStr, L'e', 25);

  //displaying the result
  if(search != NULL)
    wcout<<"Found at: "<<(search-MyStr+1)<<"\n";
  else
    wcout<<"Not Found."<<"\n";
 
  return 0;
}

The output of the above code will be:

Found at: 5

❮ C++ <cwchar> Library