# strtok ###### tags: `EOS` ex: report | park | 第一次放 s 進入 strtok 切 "切割符號" strtok(s , "|") 切割符號的字元會變成"\0" 用字元指標接: strtok 輸出到空字元前一格 `report ` report 後有空格 之後只要用0, strtok 會用之前的 buffer 繼續切 strtok(0 , "|") ` park ` park 前後都有空格 strtok(0 , "|") -> (null) 最後是空字元回傳 null ### NAME strtok, strtok_r - extract tokens from strings ### SYNOPSIS ```c #include <string.h> char *strtok(char *restrict str, const char *restrict delim); char *strtok_r(char *restrict str, const char *restrict delim, char **restrict saveptr); ``` ### DESCRIPTION The strtok() function breaks a string into a sequence of zero or more nonempty tokens. On the first call to strtok(), the string to be parsed should be specified in str. In each subsequent call that should parse the same string, str must be NULL. The delim argument specifies a set of bytes that delimit the tokens in the parsed string. The caller may specify different strings in delim in successive calls that parse the same string. Each call to strtok() returns a pointer to a null-terminated string containing the next token. This string does not include the delimiting byte. If no more tokens are found, strtok() returns NULL. ... ### RETURN VALUE The strtok() and strtok_r() functions return a pointer to the next token, or NULL if there are no more tokens. ### ATTRIBUTES | Interface | Attribute | Value | | ---------- | ------------- | --------------------- | | strtok() | Thread safety | MT-Unsafe race:strtok | | strtok_r() | Thread safety | MT-Safe | ### EXAMPLE ```c #include <stdio.h> #include <string.h> // strtok int main() { char str[] = "Hello world, nice to meet you"; const char* d = " ,"; char *p; p = strtok(str, d); while (p != NULL) { printf("%s\n", p); p = strtok(NULL, d); } return 0; } ```