How to split a string to 2 strings in C

前端 未结 8 628
猫巷女王i
猫巷女王i 2020-11-28 04:29

I was wondering how you could take 1 string, split it into 2 with a delimiter, such as space, and assign the 2 parts to 2 separate strings. I\'ve tried using strtok()

8条回答
  •  孤城傲影
    2020-11-28 05:29

    You can use strtok() for that Example: it works for me

    #include 
    #include 
    
    int main ()
    {
        char str[] ="- This, a sample string.";
        char * pch;
        printf ("Splitting string \"%s\" into tokens:\n",str);
        pch = strtok (str," ,.-");
        while (pch != NULL)
        {
            printf ("%s\n",pch);
            pch = strtok (NULL, " ,.-");
        }
        return 0;
    }
    

提交回复
热议问题