機能 |
文字列からトークンを分割する。 |
||||
---|---|---|---|---|---|
定義 |
#include <string.h> char * strtok ( char * s1, const char * s2 ); ※C99規格では以下の定義となる char * strtok ( char * restrict s1, const char * restrict s2 ); |
||||
引数 |
|
||||
戻り値 |
字句が存在した場合は、字句の最初の文字へのポインタを返す。その際、引数s1の字句が存在していた場所はナル文字(\0)で書き換えられる。 字句が存在しない場合は、NULLを返す。 |
||||
実装例 |
#include <string.h> #include <stdio.h> int main(void) { char str[] = "abc def,ghi"; char *ret; /* スペースまたは空白で分割する */ ret = strtok(str, ", "); while (NULL != ret) { printf("%s\n", ret); ret = strtok(NULL, ", "); } return 0; } |
||||
実行結果 |
abc def ghi |
strtok()