How to parse a token from a string in C?

How to parse markers from the input string. For instance:

char *aString = "Hello world". 

I want the output to be:

"Hello World"

+4
source share
5 answers

You want to use strtok - here is a good example.

+7
source

Take a look at strtok , part of the standard library.

+6
source

For repeat versions, you can use strtok_s for visual studio or strtok_r for unix

+3
source

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.

+3
source

Keep in mind that strtok is very hard to understand because:

  • He changes the input
  • The delimiter is replaced by a null terminator
  • Combines adjacent delimiters and, of course,
  • Not stream protected.

You can read about this alternative .

+2
source

All Articles