strtok函数使用

#include<string.h>
#include<stdio.h>
int main(void)
{
    char input[16]="abc,d,e";
    char*p;
    /*strtok places a NULL terminator
    infront of the token,if found*/
    p=strtok(input,",");//第一个用字符串input数组头,其余用NULL
   /*strtok()用来将字符串分割成一个个片段。参数s指向欲分割的字符串,参数delim则为分割字符串中包含的所有字符。当strtok()在参数s的字符串中发现参数delim中包含的分割字符时,则会将该字符改为\0 字符。在第一次调用时,strtok()必需给予参数s字符串,往后的调用则将参数s设置成NULL。每次调用成功则返回指向被分割出片段的指针。*/
    if(p)
        printf("%s\n",p);
        /*A second call to strtok using a NULL
        as the first parameter returns a pointer
        to the character following the token*/
    p=strtok(NULL,",");
    if(p)
        printf("%s\n",p);
    p=strtok(NULL,",");
    if(p)
        printf("%s\n",p);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/jingmin52296358al/article/details/85795427