How I tokenize a string (char array) in C
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 s
(the
index to the start of your token). s = e + 1
works in most cases.
Arguments
str
is 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_len
is the length of that string. This does not have to be the full string, just the bit you care about.s
the index of the start of your tokene
the index of the end of your tokendelim
is the character that you want to delmit your string
Return values
- length of your token
-1
when you reach the end of the string-2
when something goes wrong
Example
Result
40
4 this
6 string
3 has
3 two
9 different
10 delimiters