strtok函數用於分解字串為一組字串,strtok函數的宣告是“char *strtok(char *str, const char *delim)”,其中參數str表示要分解成一組小字串的字串。

C 函式庫函數- strtok()
描述
C 函式庫函數char *strtok (char *str, const char *delim) 分解字串str 為一組字串,delim 為分隔符號。
宣告
下面是 strtok() 函數的宣告。
char *strtok(char *str, const char *delim)
參數
str -- 要被分解成一組小字串的字串。
delim -- 包含分隔符號的 C 字串。
傳回值
該函數傳回被分解的第一個子字串,如果沒有可檢索的字串,則傳回一個空指標。
實例
下面的實例示範了 strtok() 函數的用法。
實例
#include <string.h>
#include <stdio.h>
int main () {
char str[80] = "This is - www.runoob.com - website";
const char s[2] = "-";
char *token;
/* 获取第一个子字符串 */
token = strtok(str, s);
/* 继续获取其他的子字符串 */
while( token != NULL ) {
printf( "%s\n", token );
token = strtok(NULL, s);
}
return(0);
}讓我們編譯並執行上面的程序,這將產生以下結果:
This is www.runoob.com website
推薦:《C語言教學》
以上是strtok函數的用法詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!