What is a token in C language


strtok is defined in the one that is included in C via, or in C ++ via.


strtok () is passed a changeable string and a character string of delimiters. The task of strtok is now to replace all separators with zero bytes so that they can be used more independently as a string, since each substring has a zero byte at the end. The first time strtok () is called, the function is passed the string to be broken down. strtok () internally remembers the address of the string and the position of the last separator found. With each subsequent call, NULL is passed as a string, as well as the separator. The separators can differ from the previously used separators.


#include char * strtok (char * string, charconst * delimiters);

string: Pointer to the string to be divided into.
delimiters: Characters at which the transferred string is to be subdivided

Return value: Pointer to the last token found in the string or NULL if no further tokens could be found.

Sources of error

The transferred string is divided into sub-strings by overwriting the separators with null bytes. The used memory block, however, remains in one piece and accordingly contains several substrings in a memory block. If the memory block has to be released again, the start address of the string that is transferred in strtok () has to be noted and this address has to be released as the only one. The sub-strings returned by strtok () must not be released!


#include // defines strtok #include // defines printf # include // defines EXIT_SUCCESS int main (void) {char string [] = "http: // www .proggen.org / drawer / page.html "; char delimiter1 [] =": "; char delimiter2 [] =" ./ "; char * token; printf ("Explode URL:% s \ n", string); token = strtok (string, delimiter1); while (token) {printf ("Token:% s \ n", token); token = strtok (NULL, delimiter2);} printf ("Output decomposed string:% s \ n", string); returnEXIT_SUCCESS;}


Breakdown URL: http://www.proggen.org/drawer/page.html Token: http Token: www Token: proggen Token: org Token: drawer Token: page Token: html Output of broken string: http

see also