I tried writing a string splitter for my little operating system based on http://www.daniweb.com/code/snippet318.html but it didn't quite work. Here's my code:
// BTW, this will eventually return a string containing the token_numth token. But I'm just trying to get it to work for now.
void strsplit(const char *str, char splitchar, int token_num)
{
char *delimeter = "\0";
delimeter[0] = splitchar;
int i;
strcpy(str, line);
char *token = line; /* point to the beginning of the line */
for (i = 0; *token; i++) /* loop through each character on the line */
{
/* search for delimiters */
size_t len = strcspn(token, delimeter);
token[len] = '\0';
/* print the found text: use *.* in format to specify a maximum print size */
printf("token[%2d] = \"%s\"\n", i, token);
/* advance pointer by one more than the length of the found text */
token += len + 1;
}
}
Then I call it like this:
printf("strsplit test: /meow/moo/baa/something\n");
strsplit("/meow/moo/baa/something", '/', 0);
But instead of the "meow", "moo", "baa", "something" I was expecting, I got this instead.
Popcorn 0.3 booting...
Build 60.
Looking for preloaded modules... none found
strsplit test: /meow/moo/baa/something
token[ 0] = ""
token[ 1] = "meow"
token[ 2] = "moo"
token[ 3] = "baa"
token[ 4] = "som"
token[ 5] = "thi"
token[ 6] = "g"
Any ideas on this?