C++ Standard Library C++ STL Library

C++ <cwchar> - wcspbrk() Function



The C++ <cwchar> wcspbrk() function is used to find the first occurrence of any wide character of the wide string pointed to by ws2 in the wide string pointed to by ws1. The terminating null character is considered as a part of the string but not included in searching.

The function returns pointer to the first occurrence of any wide character of ws2 in ws1, or null pointer if none of the wide characters of ws2 is present in ws1.

Syntax

const wchar_t* wcspbrk (const wchar_t* ws1, const wchar_t* ws2);
wchar_t* wcspbrk (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 containing the wide characters to match.

Return Value

Returns pointer to the first occurrence of any wide character of ws2 in ws1, or null pointer if none of the characters of ws2 is present in ws1.

Example:

The example below shows the usage of wcspbrk() 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 occurrences of 
  //any wide character of ",@#" in MyStr
  search = wcspbrk(MyStr, L",@#");

  //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: 6

❮ C++ <cwchar> Library