How to split a string to 2 strings in C

前端 未结 8 629
猫巷女王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:23

    char *line = strdup("user name"); // don't do char *line = "user name"; see Note
    
    char *first_part = strtok(line, " "); //first_part points to "user"
    char *sec_part = strtok(NULL, " ");   //sec_part points to "name"
    

    Note: strtok modifies the string, so don't hand it a pointer to string literal.

提交回复
热议问题