C++ Standard Library C++ STL Library

C++ <cwchar> - wcstok() Function



The C++ <cwchar> wcstok() function splits a null-terminated wide string pointed to by ws using the delimiter characters identified by null-terminated wide string pointed to by delim. This function is designed to be called multiple times to obtain successive tokens from the same string.

Only the first call, the function expects a C wide string as argument for ws, whose first character is used as the starting point to scan for tokens. Every subsequent call, the function expects null pointer and uses the position right after the end of the last token as the new starting location for scanning.

Syntax

wchar_t* wcstok (wchar_t* ws, const wchar_t* delim, wchar_t** ptr);               

Parameters

ws Specify pointer to the null-terminated wide string to tokenize.
delim Specify pointer to the null-terminated wide string identifying delimiters.
ptr Specify pointer to an object of type wchar_t*, which is used by wcstok to store its internal state.

Return Value

Returns pointer to the beginning of the next token or a null pointer if there are no more tokens.

Example:

The example below shows the usage of wcstok() 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 delim[] = L",#@ ";
  wchar_t * buffer;

  //getting the first token
  wchar_t * token = wcstok(MyStr, delim, &buffer);

  //searching for all tokens and displaying it
  while(token != NULL) {
    wcout<<token<<"\n";
    token = wcstok(NULL, delim, &buffer);
  }
 
  return 0;
}

The output of the above code will be:

To
be
or
not
to
be
that
is
the
question.

❮ C++ <cwchar> Library