How to split a string to 2 strings in C

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

    If you have a char array allocated you can simply put a '\0' wherever you want. Then point a new char * pointer to the location just after the newly inserted '\0'.

    This will destroy your original string though depending on where you put the '\0'

    0 讨论(0)
  • 2020-11-28 05:30

    If you're open to changing the original string, you can simply replace the delimiter with \0. The original pointer will point to the first string and the pointer to the character after the delimiter will point to the second string. The good thing is you can use both pointers at the same time without allocating any new string buffers.

    0 讨论(0)
提交回复
热议问题