strtok()是c库函数,功能是分解字符串;
声明: char* strtok(char *str,const char *delim);
参数解释: str 被分解的字符串 delim 分隔符(被分割后替换成\0)
返回值: 返回被分解的首个子串,及首个分隔符前的字符串;如果没有可检索的剩余字符,则返回空指针。
示例:(linux)
char buff[] = "a b c d e 1 2 3 6 f g";
char *str = strtok(buff," ");
printf("\n",str);
while( (str = strtok(NULL," ") )!= NULL )
{
printf("\n",str);
}
实例:©
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);
}