Why is strtok changing its input like this?

When strtok() finds a token, it changes the character immediately after the token into a \0, and then returns a pointer to the token. The next time you call it with a NULL argument, it starts looking after the separators that terminated the first token — i.e., after the \0, and possibly further along. Now, … Read more

C’s strtok() and read only string literals

What did you initialize the char * to? If something like char *text = “foobar”; then you have a pointer to some read-only characters For char text[7] = “foobar”; then you have a seven element array of characters that you can do what you like with. strtok writes into the string you give it – … Read more

strtok segmentation fault

The problem is that you’re attempting to modify a string literal. Doing so causes your program’s behavior to be undefined. Saying that you’re not allowed to modify a string literal is an oversimplification. Saying that string literals are const is incorrect; they’re not. WARNING : Digression follows. The string literal “this is a test” is … Read more

How to split a string to 2 strings in C

#include <string.h> char *token; char line[] = “SEVERAL WORDS”; char *search = ” “; // Token will point to “SEVERAL”. token = strtok(line, search); // Token will point to “WORDS”. token = strtok(NULL, search); Update Note that on some operating systems, strtok man page mentions: This interface is obsoleted by strsep(3). An example with strsep … Read more

C: correct usage of strtok_r

The documentation for strtok_r is quite clear. The strtok_r() function is a reentrant version strtok(). The saveptr argument is a pointer to a char * variable that is used internally by strtok_r() in order to maintain context between successive calls that parse the same string. On the first call to strtok_r(), str should point to … Read more

Split string into tokens and save them in an array

#include <stdio.h> #include <string.h> int main () { char buf[] =”abc/qwe/ccd”; int i = 0; char *p = strtok (buf, “https://stackoverflow.com/”); char *array[3]; while (p != NULL) { array[i++] = p; p = strtok (NULL, “https://stackoverflow.com/”); } for (i = 0; i < 3; ++i) printf(“%s\n”, array[i]); return 0; }

Using strtok in c

Here is my take at a reasonably simple tokenize helper that stores results in a dynamically growing array null-terminating the array keeps the input string safe (strtok modifies the input string, which is undefined behaviour on a literal char[], at least I think in C99) To make the code re-entrant, use the non-standard strtok_r #include … Read more

Nested strtok function problem in C [duplicate]

You cannot do that with strtok(); use strtok_r() from POSIX or strtok_s() from Microsoft if they are available, or rethink your design. char *strtok_r(char *restrict s, const char *restrict sep, char **restrict lasts); char *strtok_s(char *strToken, const char *strDelimit, char **context); These two functions are interchangeable. Note that a variant strtok_s() is specified in an … Read more