The first time you call strtok, the string is passed as the first argument. Subsequent calls must pass NULL as the first argument.

Code:

char names[] = "This is a test"; /* String MUST be modifiable */

/* First call initializes strtok with our string and returns the first token in the string */
buffer = strtok(names, " ");

while (buffer != NULL)
{
printf("%s", buffer);
/* Now we pass NULL to strtok to get the next token in the string */
buffer = strtok(NULL, " ");
}

05-02-2004

Cbuild

thanks however, that still doesn't parse my string. I think the issue might be with the algorithm I'm trying to use. Basically the end result is to seperate each of the names and then look at them individouly. I'll continue playing around with strtok but if anybody has an idea about how to parse this let me know.

05-02-2004

Stack Overflow

Hi CBuild,

I have written code similar to this before. It may not be the best, but it may help and contribute to what you are looking for. I will simply write a function called parseString(char *, char[][], char); which simply takes the source string, finds your designated char or in this case, a space, and then it seperates them putting them into their own 2-Dimensional char[][].

Ok, for an explanation of parseString(), I decided to use a 2-D array instead of a char *[] because I didn't see a need to allocate memory for this small task. Now if course this will be handling large strings and unknown size array's I would recommend you to use malloc(), free(), etc...

I simply take the *src, search for find, and seperate the lines inserting the data into dest[][]. If you have any question, please feel free to ask, I put some comments in there so you knew what each step was about.

Hope this helps,
- Stack Overflow

05-03-2004

Cbuild

Thanks for that function. I'm going to have to modify it slightly for my specific problem but this does pretty much what i wanted.

I'll prolly end up going with the char *[] rather than the char [][] ..since i might have 5 names in that string or 500 names!

05-03-2004

quzah

Quote:

I'll prolly end up going with the char *[] rather than the char [][] ..since i might have 5 names in that string or 500 names!

It's the same thing.

Code:

void foo1( char bar1[][SIZE] );

void foo2( char *bar2[SIZE] );

They have the same effect. Just omit the first size index size of the array, which tells basicly says "I don't know how many there are, but each one is SIZE big."