For reasons that now escape me, I stopped using
strtok to parse strings in
C. I can only guess that it was because
strtok changes the original string,
making it difficult to re-parse, and that alternating between delimiters was
hard. These may no longer be issues with
strok, I have may have been using
strtok incorrectly, and there may be better functions I should have used.
In any case, here is
scan_s. It is simple, does not modify the base string,
and lends itself well to the nested parsing of different delimiters. A
slightly annoying part of
scan_s is that YOU MUST increment
index to the start of your token).
s = e + 1 works in most cases.
stris a pointer to the beginning of your string. More specifically, it is a pointer to the position first character in some string that you want to parse.
str_lenis the length of that string. This does not have to be the full string, just the bit you care about.
sthe index of the start of your token
ethe index of the end of your token
delimis the character that you want to delmit your string
- length of your token
-1when you reach the end of the string
-2when something goes wrong
40 4 this 6 string 3 has 3 two 9 different 10 delimiters