C - split string into an array of strings

后端 未结 2 1861
囚心锁ツ
囚心锁ツ 2020-11-27 03:42

I\'m not completely sure how to do this in C:

char* curToken = strtok(string, \";\");
//curToken = \"ls -l\" we will say
//I need a array of strings contain         


        
2条回答
  •  爱一瞬间的悲伤
    2020-11-27 03:50

    Here is an example of how to use strtok borrowed from MSDN.

    And the relevant bits, you need to call it multiple times. The token char* is the part you would stuff into an array (you can figure that part out).

    char string[] = "A string\tof ,,tokens\nand some  more tokens";
    char seps[]   = " ,\t\n";
    char *token;
    
    int main( void )
    {
        printf( "Tokens:\n" );
        /* Establish string and get the first token: */
        token = strtok( string, seps );
        while( token != NULL )
        {
            /* While there are tokens in "string" */
            printf( " %s\n", token );
            /* Get next token: */
            token = strtok( NULL, seps );
        }
    }
    

提交回复
热议问题