Open In App

wcstok() function in C++ with example

Improve
Improve
Like Article
Like
Save
Share
Report

The wcstok() function is defined in cwchar.h header file. The wcstok() function returns the next token in a null terminated wide string. The pointer delim points to the separator characters i.e. the delimiter.

.
Syntax: 
 

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

Parameters: This method takes following parameters: 
 

  • str: It represents pointer to the null-terminated wide string to tokenize.
  • delim: It represents pointer to the null terminated wide string that contains the separators.
  • ptr: It represents pointer to an object of type wchar_t*, which is used by wcstok to store its internal state.

Return Value: The wcstok() function returns the pointer to the beginning of next token if there is any. Otherwise it returns zero.
Below program illustrate the above function:
Example 1:
 

cpp




// c++ program to demonstrate
// example of wcstok() function.
 
#include <bits/stdc++.h>
using namespace std;
 
int main()
{
 
    // Get the string
    wchar_t str[] = L"A computer science portal for geeks";
 
    // Creating the parameters of wcstok() method
 
    // Create the pointer of which
    // the next token is required
    wchar_t* ptr;
 
    // Define the delimiter of the string
    wchar_t delim[] = L" ";
 
    // Call the wcstok() method
    wchar_t* token = wcstok(str, delim, &ptr);
 
    // Print all tokens with the help of it
    while (token) {
        wcout << token << endl;
        token = wcstok(NULL, delim, &ptr);
    }
 
    return 0;
}


Output: 

A
computer
science
portal
for
geeks

 


Last Updated : 12 Dec, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads