March 14th, 2013, 07:44 PM
Using STRTOK with different variables
lets say i have a variable declared as follows
char str = "hello what's up?";
and I want to use strtok, how can i store each work in a different variable using strtok?
March 14th, 2013, 09:01 PM
I recommend an array. You'll only have to use an array once after several times writing
to realize that arrays are a better concept. Better than sliced bread. Wait! Arrays are like sliced bread. Well, a little bit.
work1 = strtok(args);
work2 = strtok(other args);
work3 = strtok(more args);
/* oh no now I need to process a 6 work sentence */
Last edited by b49P23TIvg; March 14th, 2013 at 09:02 PM.
Reason: Tense change by inserting a missing character.
[/code] are essential for python code and Makefiles!
March 14th, 2013, 09:45 PM
Read the documentation for strtok, which you can find by Google'ing on man page strtok; eg at http://man7.org/linux/man-pages/man3/strtok.3.html.
Also, we're not sure what you're asking here.
Are you asking how to extract multiple tokens out of the same string? If that's the case, then understand how strtok works and how you're supposed to use it.
strtok works with an internal static buffer. So the first time you call it, you need to pass in the string you want it to tokenize, in which case it will load that string into that internal static buffer. Then to continue working with that same string, you pass in NULL. And to start working with a different string, you pass in that new string instead of NULL.
The return value of strtok is a pointer to the token that was found, or NULL if none was found. So to get the next token, you call strtok again. If I understand what you want, each time you call strtok, you need to save the token that is returned to you. Now, strtok changes the original string that you pass to it and I think that it replaces the delimiter it finds with a null-terminator, but don't quote me on that. The way that we use strtok is to either immediately process the token (eg, convert a number token to an int) or to strcpy the token to a string.
Now, if you instead want to be tokenizing more than one string at the same time, then you can't do that with strtok because of that static internal buffer. However, you may be able to do it with the reentrant version, strtok_r. I'm not familiar with that one, but you can read up on it in the man page.
March 15th, 2013, 04:45 AM
strtok works "in-place" by replacing delimiters with nul and returning a pointer to the start of terminated substring. If you want the data in strings associated with variables you can either assign each extracted token to a pointer variable (zero-copy) or use strcpy() to move the content do an array variable.