![]() |
In C, tokenization is the process of breaking the string into smaller parts using delimiters (characters treated as separators) like space, commas, a specific character, or even a string. Those smaller parts are called tokens where each token is a substring of the original string separated by the delimiter. Tokenizing can be done manually using user-defined functions but C language also provides a built-in function strtok() defined inside <string.h> standard string library to simplify this process. strtok() FunctionThe strtok() function splits the string into substrings by separating the original string using delimiters. It is defined inside the <string.h> header so we need to include it before using the strtok() function. Syntaxchar* strtok(char *str, const char *delimiter) Parameters
How to use strtok() function?Initially, when we pass all the parameters of the string, this function will return only the first token of the input string. After that, we need to pass “NULL” instead of the input string to obtain the following tokens. It will return NULL when there is no token left. For Example, Assume that the input original string is “This is an example string” and the delimiter is ” ” (space). If we pass these parameters to the strtok() function, the function will return the first token “This”. To obtain the remaining tokens, we need to pass “NULL” instead of the input string. If we pass “NULL”, the strtok() will continue from where it left. In the last step, strtok() returned the first token “This”. so, if we “NULL” it will continue from the next token “is” and so on until there are no more tokens left. Examples of String Tokenization in CExample 1:C
Output
Initial String: GEEKS-FOR-GEEKS After Tokenization: GEEKS FOR GEEKS Example 2:C
Output
Token: This Token: is Token: an Token: example Token: string |
Reffered: https://www.geeksforgeeks.org
C++ |
Related |
---|
![]() |
![]() |
![]() |
![]() |
![]() |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 14 |