C++ Standard Library C++ STL Library

C++ <cwchar> - wcschr() Function



The C++ <cwchar> wcschr() function is used to find the first occurrence of a wide character wc in the wide string pointed to by ws. The terminating null character is considered as a part of the string and can be found if searching for '\0'.

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

Syntax

const wchar_t* wcschr (const wchar_t* ws, wchar_t wc);
wchar_t* wcschr (wchar_t* ws, wchar_t wc);              

Parameters

ws Specify pointer to the null-terminated wide string to be searched in.
wc Specify the wide character to searched for.

Return Value

Returns pointer to the first occurrence of wc in ws, or null pointer if wc is not found in ws.

Example:

The example below shows the usage of wcschr() 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 all occurrences of 'o' in MyStr
  wcout<<"Searching for all occurrences of 'o' in MyStr.\n";
  search = wcschr(MyStr, L'o');

  //displaying the result
  while(search != NULL) {
    wcout<<"Found at: "<<(search-MyStr+1);
    wcout<<endl;
    search = wcschr(search+1, 'o');
  }
 
  return 0;
}

The output of the above code will be:

Searching for all occurrences of 'o' in MyStr.
Found at: 2
Found at: 8
Found at: 12
Found at: 16
Found at: 40

❮ C++ <cwchar> Library