How does strtok() split the string into tokens in C?

前端 未结 15 1879
陌清茗
陌清茗 2020-11-22 14:48

Please explain to me the working of strtok() function. The manual says it breaks the string into tokens. I am unable to understand from the manual what it actua

相关标签:
15条回答
  • 2020-11-22 15:47

    strtok() divides the string into tokens. i.e. starting from any one of the delimiter to next one would be your one token. In your case, the starting token will be from "-" and end with next space " ". Then next token will start from " " and end with ",". Here you get "This" as output. Similarly the rest of the string gets split into tokens from space to space and finally ending the last token on "."

    0 讨论(0)
  • 2020-11-22 15:49

    strtok replaces the characters in the second argument with a NULL and a NULL character is also the end of a string.

    http://www.cplusplus.com/reference/clibrary/cstring/strtok/

    0 讨论(0)
  • 2020-11-22 15:50

    strtok modifies its input string. It places null characters ('\0') in it so that it will return bits of the original string as tokens. In fact strtok does not allocate memory. You may understand it better if you draw the string as a sequence of boxes.

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