Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c

What is wcstok_s in C?

Samia Ishaque

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Answers Code

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);

Parameters

The wcstoke_s() function has four parameters that must be passed:

  1. str: a pointer to the null-terminated string we wish to tokenize.
  2. strmax: a pointer to a type rsize_t that stores the size of the str that is still left to be parsed.
  3. delim: a pointer to the null-terminated string which represents the delimiters (we will tokenize the str according to delim).
  4. 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.

Return value

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.

Example

Note: The function wcstok_s() is not supported by the GCC compiler, so you will get an implicit declaration of function… error. Use the wcstok() variant to get the job done. You can read more about wcstok() 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 tokenize
wchar_t str_to_tokenize[] = L"Educative is a great platform";
//the pointer stroing state of the parser
wchar_t *state;
wchar_t *token = wcstok(str_to_tokenize, L" ", &state);
//keep tokenizing until token=NULL
while(token){
printf("%ls\n", token);
token = wcstok(NULL, L" ", &state);
}
return 0;
}

Explanation

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.

RELATED TAGS

c

CONTRIBUTOR

Samia Ishaque
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Answers Code
Keep Exploring