天天看點

C語言:使用strtok()函數分割字元串

簡單的分割字元串

#include <stdio.h>
#include <string.h>

int main()
{
    char str[];
    char split[] = ",. /';\\";//分割符号

    printf("please input\n");
    fgets(str, sizeof str, stdin);

    int* p = strtok(str, split);//strtok()傳回單元化後的指針
    printf("%s\n", p);
    //循環單元化後續的字元串,第一個參數置空NULL
    while ((p = strtok(NULL, split)) != NULL)
    {
        printf("%s\n", p);
    }
    getchar();//按任意鍵繼續
    return ;
}
           

繼續閱讀