Strtok to separate all whitespace
30,545
Here is an example that illustrates that strtok() will work on tabs or spaces. The key is to pass in NULL on the all but the first call to strtok().
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
char buffer[1024];
int rv = sprintf(buffer, "a string\ttokenize.");
char *token = strtok(buffer, " \t");
int i = 0;
printf("cnt token\n");
printf("==========\n");
while (token) {
printf("%2d %s\n", i++, token);
token = strtok(NULL, " \t");
}
return 0;
}
output from above program is as follows below.
cnt token
==========
0 a
1 string
2 tokenize.
Author by
Haskell
Updated on July 30, 2022Comments
-
Haskell almost 2 years
I'm trying to split a string at spaces and tabs.
char * token = strtok(input, " \t");
works only for spaces. What am I doing wrong?
-
marc.soda almost 4 yearsThis answer is perfect. I know it's a bit late, but I believe there is a type: within the while loop, it should be "*token" instead of "token".
-
User113 over 3 yearsI do not believe this is the case @marc.soda Token is a string (char array), so de-referencing would only attempt to put the token in the first place in the array. This is not the desired behavior.