C++ Standard Library C++ STL Library

C++ <cwchar> - wcsstr() Function



The C++ <cwchar> wcsstr() function is used to find the first occurrence of the wide string ws2 in the wide string pointed to by ws1. The terminating null characters are not compared.

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

Syntax

const wchar_t* wcsstr (const wchar_t* ws1, const wchar_t* ws2);
wchar_t* wcsstr (wchar_t* ws1, const wchar_t* ws2);                 

Parameters

ws1 Specify pointer to the null-terminated wide string to be searched in.
ws2 Specify pointer to the null-terminated wide string to be searched for.

Return Value

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

Example:

The example below shows the usage of wcsstr() 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 "be"
  search = wcsstr(MyStr, L"be");

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

The output of the above code will be:

First occurrence of 'be' starts at: 4

❮ C++ <cwchar> Library