How to split a string to 2 strings in C
Solution 1
#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
is shown below:
char* token;
char* string;
char* tofree;
string = strdup("abc,def,ghi");
if (string != NULL) {
tofree = string;
while ((token = strsep(&string, ",")) != NULL)
{
printf("%s\n", token);
}
free(tofree);
}
Solution 2
For purposes such as this, I tend to use strtok_r() instead of strtok().
For example ...
int main (void) {
char str[128];
char *ptr;
strcpy (str, "123456 789asdf");
strtok_r (str, " ", &ptr);
printf ("'%s' '%s'\n", str, ptr);
return 0;
}
This will output ...
'123456' '789asdf'
If more delimiters are needed, then loop.
Hope this helps.
Solution 3
char *line = strdup("user name"); // don't do char *line = "user name"; see Note
char *first_part = strtok(line, " "); //first_part points to "user"
char *sec_part = strtok(NULL, " "); //sec_part points to "name"
Note: strtok
modifies the string, so don't hand it a pointer to string literal.
Solution 4
You can use strtok() for that Example: it works for me
#include <stdio.h>
#include <string.h>
int main ()
{
char str[] ="- This, a sample string.";
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;
}
Solution 5
If you have a char array allocated you can simply put a '\0'
wherever you want.
Then point a new char * pointer to the location just after the newly inserted '\0'
.
This will destroy your original string though depending on where you put the '\0'
Related videos on Youtube
Mark Szymanski
Updated on May 09, 2020Comments
-
Mark Szymanski about 4 years
I was wondering how you could take 1 string, split it into 2 with a delimiter, such as space, and assign the 2 parts to 2 separate strings. I've tried using
strtok()
but to no avail.-
Drew Delano over 14 yearsShow us your strtok() attempt.
-
-
John Bode over 14 years
strtok()
modifies its input, so using it on a string literal is bad juju (a.k.a undefined behavior). -
John Bode over 14 yearsLooks good, except the second strtok() call is using a different delimiter.
-
ereOn over 14 yearsYep. I forgot to mention that. Thanks.
-
Drew Delano over 14 years@ereOn: Perhaps you missed the point. Your example is passing a pointer to a string literal, therefore strtok() will be modifying the string literal and invoking UB.
-
thecoshman over 14 yearsWhat's the difference betwen strtok_r() and strtok()?
-
ereOn over 14 yearsMy point was to demonstrate the basic use of strtok(). Well, I admit that using string literals in this situation is a bad pratice and should be avoided. Fun fact: if you look at cplusplus.com/reference/clibrary/cstring/strtok you can see the same "mistake" in the example.
-
Drew Delano over 14 yearsI think if you change
char *line = "SEVERAL WORDS"
tochar line[] = "SEVERAL WORDS"
, you're all set. -
ereOn over 14 yearsIf this is true, I just learned something. I thought those two instructions had the same meaning. My bad.
-
David Thornley over 14 years@ereOn: Pointers and arrays are different things in C, until you sneeze near an array, and then it turns into a pointer. That's how the array size expression
sizeof(arr)/sizeof(arr[0])
works. -
ereOn over 14 yearsstrtok_r() is the reentrant version of strtok(). More about reentrancy here: en.wikipedia.org/wiki/Reentrant_%28subroutine%29
-
pmg over 11 years
strtok()
is obsoleted? WOW!strsep()
is not even Standard C. -
Bitterblue over 11 years@ereOn Is it normal that strtok and strsep are throwing error signals ? Is there a way to avoid errors ? None of the code above worked without errors for me.
-
ereOn over 11 years@mini-me: I never heard of error signals using those functions. You probably should ask your own question (referring to this one, if it is relevant).