The wcstok_s()
function in C language breaks down a string into multiple tokens based on the delimiter passed to the function. wcstok_s()
is defined in the <wchar.h>
header file, so it is important to include this library at the beginning of your program as shown below:
#include<wchar.h>
The wcstok_s()
function is defined as the following:
wchar_t *wcstok_s( wchar_t *restrict str, rsize_t *restrict strmax,
const wchar_t *restrict delim, wchar_t **restrict ptr);
The wcstoke_s()
function has four parameters that must be passed:
str
: a pointer to the null-terminated string we wish to tokenize.strmax
: a pointer to a type rsize_t
that stores the size of the str
that is still left to be parsed.delim
: a pointer to the null-terminated string which represents the delimiters (we will tokenize the str
according to delim
).ptr
: a pointer of type wchar_t
which stores the state of the parser. This means that it keeps track of how far a string’s been parsed.The return value is a pointer of type wchar_t
which points to the next token obtained from tokenizing or a null value to indicate that the string has been fully tokenized.
Note: The function
wcstok_s()
is not supported by the GCC compiler, so you will get animplicit declaration of function…
error. Use thewcstok()
variant to get the job done. You can read more aboutwcstok()
here.
Below is an example of how you can use the wcstok()
(a variant of wcstok_s()
) function:
#include<stdio.h>#include<wchar.h>int main(){//the string we wish to tokenizewchar_t str_to_tokenize[] = L"Educative is a great platform";//the pointer stroing state of the parserwchar_t *state;wchar_t *token = wcstok(str_to_tokenize, L" ", &state);//keep tokenizing until token=NULLwhile(token){printf("%ls\n", token);token = wcstok(NULL, L" ", &state);}return 0;}
In the above code, the string we wish to tokenize is defined as str_to_tokenize
of the type wchar_t
. We also define a pointer of type wchar_t
to store the state of the parser, which indicates how far the string’s been parsed. Then, we tokenize the string by setting ‘space’ as a delimiter; this will tokenize the string into each of its words. The return value is stored in the variable token
to keep track of the next token value returned. We use a while
loop to keep tokenizing the string until token
is equal to NULL
, as the string has ended. We can also print each one of the tokens within the while loop to see how the tokenizing takes place.
Free Resources