How do i parse tokens from an input string. For example:
char *aString = "Hello world".
I want the output to be:
"Hello" "world"
How do i parse tokens from an input string. For example:
char *aString = "Hello world".
I want the output to be:
"Hello" "world"
For re-entrant versions you can either use strtok_s for visual studio or strtok_r for unix
Keep in mind that strtok is very hard to get it right, because:
You can read about this alternative.
strtok
is the easy answer, but what you really need is a lexer that does it properly. Consider the following:
As you can see, writing a proper lexer is not straightforward, and strtok
is not a proper lexer.
Other solutions could be a single character state machine that does precisely what you need, or regex-based solution that makes locating words versus gaps more generalized. There are many ways.
And of course, all of this depends on what your actual requirements are, and I don't know them, so start with strtok
. But it's good to be aware of the various limitations.