strtok is a simple answer, but you really need a lexer that does it right. Consider the following:
- Is there one or two spaces between hello and the world?
- can it be any number of spaces?
- can include vertical spaces (\ n, \ f, \ v) or just horizontal ones (\ s, \ t, \ r)?
- can include any space characters UNICODE?
- if there were punctuation between the words ("hello world"), would punctuation be a separate marker, a part of "hello" or ignored?
As you can see, writing the right lexer is not easy, and strtok not the right lexer.
Other solutions may be single-character end machines that do exactly what you need, or a regular expression solution that makes word searching easier than spaces. There are many ways.
And, of course, it all depends on your actual requirements, and I don’t know them, so start with strtok . But it’s good to know the various limitations.
source share