調用方式:char *strtok(char *str1,char *str2);
功能說明:函數strtok()返回字符串str1中指向一個由str2所指定的字符或者字符串的分隔符的指針,當沒有要返回的分隔符時,就返回一個空指針。
函數strtok()實際上修改了有str1指向的字符串。每次找到一個分隔符後,一個空(NULL)就被放到分隔符處,函數用這種方法來連續查找該字符串。
例子:
#include <string.h>
#include <stdio.h>
int main( int argc, char *argv[] )
{
char *p;
char str[100]="This is a test,and you can use it";
p = strtok(str," "); //注意,此時得到的 p爲指向字符串:"This",即在第一個分隔符前面的字符串,即每次找到一個分隔符後,一個空(NULL)就被放到分隔符處,所以此時NULL指針指向後面的字符串:"is a test ,and you can use it"。
printf("%s\n",p); //此時顯示:This
do
{
p = strtok(NULL, ","); //NULL即爲上面返回的指針,即字符串:"is a test ,and you can use it"。
if(p)
printf("|%s",p);
}while(p);
功能說明:函數strtok()返回字符串str1中指向一個由str2所指定的字符或者字符串的分隔符的指針,當沒有要返回的分隔符時,就返回一個空指針。
函數strtok()實際上修改了有str1指向的字符串。每次找到一個分隔符後,一個空(NULL)就被放到分隔符處,函數用這種方法來連續查找該字符串。
例子:
#include <string.h>
#include <stdio.h>
int main( int argc, char *argv[] )
{
char *p;
char str[100]="This is a test,and you can use it";
p = strtok(str," "); //注意,此時得到的 p爲指向字符串:"This",即在第一個分隔符前面的字符串,即每次找到一個分隔符後,一個空(NULL)就被放到分隔符處,所以此時NULL指針指向後面的字符串:"is a test ,and you can use it"。
printf("%s\n",p); //此時顯示:This
do
{
p = strtok(NULL, ","); //NULL即爲上面返回的指針,即字符串:"is a test ,and you can use it"。
if(p)
printf("|%s",p);
}while(p);
return 0;
}