I am trying to split strings with strtok, but I can't print the second part of my string.
int main (void) {
char str[128];
char *ptr;
strcpy (str, "123456 789asdf");
strtok (str, " ", &ptr);
printf ("'%s' '%s'\n", str, ptr);
return 0;
}
the outcome is something like this:
'123456' '12jfs90123#4'
It looks to me like you've misunderstood how strtok works.
The second parameter is supposed to point to a string of delimiter characters. The "current" string is the return value. On the first call, you pass the string you want to split. On subsequent calls, you pass NULL for the first parameter.
strtok also modifies the string you pass to it, so you must not ever pass a string literal as the first parameter.
Working code normally looks at least a little more like this:
char input[] = "123456 789asdf";
char *substring = strtok(input, " ");
do {
printf("%s\n", substring);
} while (strtok(NULL, " "));
If, however, you know you're only going to look for two arguments, it's probably easier to use scanf:
char s1[256], s2[256];
sscanf(str, "%255s %255s", s1, s2);
You need to keep calling strtok until it returns NULL, as strtok returns one token resulting from the splitting at every function call.
This is working code:
int main (void) {
char str[128];
char *ptr;
strcpy (str, "123456 789asdf");
ptr = strtok (str, " ");
while(ptr != NULL)
{
printf ("'%s'\n", ptr);
ptr = strtok(NULL, " ");
}
return 0;
}
strtok() works a bit differently than the way you used it:
char str[] = "123456 789asdf";
char* token = strtok (str, " ");
char* token2 = strtok (NULL, " ");
printf ("'%s' '%s'\n", token, token2);
outputs the desired: '123456' '789asdf'
Related
To understand the behavior of strtok() in C ANSI, I worte two code.
#include <stdio.h>
#include <string.h>
int main()
{
char str[101] = "This is";
char *pch;
printf("Splitting string %s into tokens : \n",str);
pch = strtok(str," ");`enter code here`
while(pch != NULL)
{
printf("%s\n",pch);
pch = strtok(NULL, " ");
}
return 0;
}
The result of This program is
Splitting string "This is " into tokens:
This
is
Next, I changed it a little bit.
#include <stdio.h>
#include <string.h>
int main()
{
char str[101] = ;
char *pch;
scanf("%s",str); //After launch program, I typed "This is "
str[strcspn(str,"\n")] = '\0'
printf("Splitting string %s into tokens : \n",str);
pch = strtok(str," ");`enter code here`
while(pch != NULL)
{
printf("%s\n",pch);
pch = strtok(NULL, " ");
}
return 0;
}
It prints
Splitting string "This" into tokens:
This
I can't understand why the second word is gone when I use stdin.
The problem isn't with strtok, but with your use of scanf and the "%s" format specifier. That format specifier reads space delimited strings, i.e you can not use "%s" to read anything with a space in it.
The natural solution is to use fgets instead, which you have already prepared for by "removing the newline" (which scanf would not usually read anyway).
It should have been pretty obvious that the strtok can't be involved, since you print the input string before even calling strtok.
I have this code in my program:
char* tok = NULL;
char move[100];
if (fgets(move, 100, stdin) != NULL)
{
/* then split into tokens using strtok */
tok = strtok(move, " ");
while (tok != NULL)
{
printf("Element: %s\n", tok);
tok = strtok(NULL, " ");
}
}
I have tried adding printf statements before and after fgets, and the one before gets printed, but the one after does not.
I cannot see why this fgets call is causing a segmentation failure.
If someone has any idea, I would much appreciate it.
Thanks
Corey
The strtok runtime function works like this
the first time you call strtok you provide a string that you want to tokenize
char s[] = "this is a string";
in the above string space seems to be a good delimiter between words so lets use that:
char* p = strtok(s, " ");
what happens now is that 's' is searched until the space character is found, the first token is returned ('this') and p points to that token (string)
in order to get next token and to continue with the same string NULL is passed as first argument since strtok maintains a static pointer to your previous passed string:
p = strtok(NULL," ");
p now points to 'is'
and so on until no more spaces can be found, then the last string is returned as the last token 'string'.
more conveniently you could write it like this instead to print out all tokens:
for (char *p = strtok(s," "); p != NULL; p = strtok(NULL, " "))
{
puts(p);
}
EDITED HERE:
If you want to store the returned values from strtok you need to copy the token to another buffer e.g. strdup(p); since the original string (pointed to by the static pointer inside strtok) is modified between iterations in order to return the token.
I am not the best with pointers, so maybe you can see what I'm doing wrong.
Let's say that I have an array that was initialized like this:
char *arrayOfCommands[]={"ls -l", "wc -l"};
My goal is to get an array called char *currentCommand out of this array that looks at a specific cell of arrayOfCommands and separates the command into pieces on spaces.
My final goal would be to have a new currentCommand array on each loop that each look like this:
First Loop:
currentCommand = [ls][-l]
First Loop:
currentCommand = [wc][-l]
Here is the code I have so far:
for (i = 0; i < 2; ++i) {
char str[] = arrayOfCommands[i];
char * currentCommand;
printf ("Splitting string \"%s\" into tokens:\n",str);
currentCommand = strtok (str, " ");
while (currentCommand != NULL){
printf ("%s\n",currentCommand);
currentCommand = strtok (NULL, " ");
}
.
.
.
//Use the currentCommand array (and be done with it)
//Return to top
}
Any help would be greatly appreciated! :)
UPDATE:
for (i = 0; i < commands; ++i) {
char str[2];
strncpy(str, arrayOfCommands[i], 2);
char *currentCommand[10];
printf ("Splitting string \"%s\" into tokens:\n",str);
currentCommand = strtok (str, DELIM);
while (currentCommand != NULL){
printf ("%s\n",currentCommand);
currentCommand = strtok (NULL, DELIM);
}
}
I am getting this error: ** incompatible types in assignment**
It's talking about the "str" I'm passing the strtok function.
strtok operates by modifying the string that you pass; this is easy to miss when using some man pages. Each command in your array is a literal string: attempts to modify them will cause problems. So you'll need to make a copy of each command before using it with strtok.
Furthermore, this is an invalid initialization for an array:
char str[] = arrayOfCommands[i];
Declare str as an array of some fixed size, then use strncpy to make copies of each command before tokenizing them using strtok:
char str[MAX_COMMAND_LEN + 1];
strncpy(str, arrayOfCommands[i], MAX_COMMAND_LEN);
// ...
Is there any pre-defined function in C that can split a string given a delimeter? Say I have a string:
"Command:Context"
Now, I want to store "Command" and "Context" to a two dimensional array of characters
char ch[2][10];
or to two different variables
char ch1[10], ch2[10];
I tried using a loop and it works fine. I'm just curious if there is such function that already exists, I don't want to reinvent the wheel. Please provide a clear example, thank you very much!
You can use strtok
Online Demo:
#include <stdio.h>
#include <string.h>
int main ()
{
char str[] ="Command:Context";
char * pch;
printf ("Splitting string \"%s\" into tokens:\n",str);
pch = strtok (str,":");
while (pch != NULL)
{
printf ("%s\n",pch);
pch = strtok (NULL, ":");
}
return 0;
}
Output:
Splitting string "Command:Context" into tokens:
Command
Context
You can tokenise a string with strtok as per the following sample:
#include <stdio.h>
#include <string.h>
int main (void) {
char instr[] = "Command:Context";
char words[2][10];
char *chptr;
int idx = 0;
chptr = strtok (instr, ":");
while (chptr != NULL) {
strcpy (words[idx++], chptr);
chptr = strtok (NULL, ":");
}
printf ("Word1 = [%s]\n", words[0]);
printf ("Word2 = [%s]\n", words[1]);
return 0;
}
Output:
Word1 = [Command]
Word2 = [Context]
The strtok function has some minor gotchas that you probably want to watch out for. Primarily, it modifies the string itself to weave its magic so won't work on string literals (for example).
I am trying to work with strtok and strcat but the second printf never shows up. Here is the code:
int i = 0;
char *token[128];
token[i] = strtok(tmp, "/");
printf("%s\n", token[i]);
i++;
while ((token[i] = strtok(NULL, "/")) != NULL) {
strcat(token[0], token[i]);
printf("%s", token[i]);
i++;
}
If my input is 1/2/3/4/5/6 for tmp then the console output would be 13456. The 2 is always missing. Does anyone know how to fix this?
The two is always missing because on the first iteration of your loop you overwrite it with the call to strcat.
After entry to the loop your buffer contains: "1\02\03/4/5/6" internal strtok pointer is pointing to "3". tokens[1] points to "2".
You then call strcat: "12\0\03/4/5/6" so your token[i] pointer is pointing to "\0". The first print prints nothing.
Subsequent calls are OK because the null characters do not overwrite the input data.
To fix it you should build up your output string into a second buffer, not the one you are parsing.
A working(?) version:
#include <stdio.h>
#include <string.h>
int main(void)
{
int i = 0;
char *token[128];
char tmp[128];
char removed[128] = {0};
strcpy(tmp, "1/2/3/4/5/6");
token[i] = strtok(tmp, "/");
strcat(removed, token[i]);
printf("%s\n", token[i]);
i++;
while ((token[i] = strtok(NULL, "/")) != NULL) {
strcat(removed, token[i]);
printf("%s", token[i]);
i++;
}
return (0);
}
strtok modifies the input string in place and returns pointers to that string. You then take one of those pointers (token[0]) and pass it to another operation (strcat) that writes to that pointer. The writes are clobbering each other.
If you want to concatenate all the tokens, you should allocate a separate char* to strcpy to.