tags:

views:

141

answers:

6
+6  Q: 

Parsing a string

i have a string of the format "ABCDEFG,12:34:56:78:90:11". i want to separate these two values that are separated by commas into two different strings. how do i do that in gcc using c language.

+7  A: 

One possibility is something like this:

char first[20], second[20];

scanf("%19[^,], %19[^\n]", first, second);
Jerry Coffin
darn! i wanted to upvote but had an accident with wireless mouse and can't vote on this answer anymore! please consider this comment as +1 :)
atzz
+1 for the simplest solution and for using it safely.
jweyrich
+3  A: 
char str[] = "ABCDEFG,12:34:56:78:90:11"; //[1]

char *first = strtok(str, ",");  //[2]
char *second = strtok(NULL, "");  //[3]

[1]  ABCDEFG,12:34:56:78:90:11  

[2]  ABCDEFG\012:34:56:78:90:11
     Comma replaced with null character with first pointing to 'A'

[3]  Subsequent calls to `strtok` have NULL` as first argument.
     You can change the delimiter though.

Note: you cannot use "string literals", because `strtok` modifies the string.
N 1.1
i get segmentation fault when trying this out? what may be the reason?
sfactor
@sfactor -- you are probably calling `strtok("ABCD,etc.", ",")` -- i.e. passing a string literal to `strtok`. And `strtok` works by modifying the original string -- it replaces separators (',') with null characters. This is one of the reasons I wouldn't recommend `strtok` even to my enemy :). (The second reason being that it keeps a global state and thus not reentrant and can be not thread-safe).
atzz
@sfactor: apart from what atzz has already said, make sure the string you call strtok on, is null terminated. can you post exactly what are you using?
N 1.1
@atzz: True, but OP does not require thread-safe code (do you OP?) and IMO OP mentions "i want to separate these two values that are separated by commas into two different strings", so `strtok` seems good option to me.
N 1.1
Your second line can actually be `char *second = strtok(NULL, "");` to fetch the remainder of the string.
caf
@caf: noted. thanks.
N 1.1
+1  A: 

You can use strtok which will allow you to specify the separator and generate the tokens for you.

nevets1219
+1  A: 

You could use strtok:

Example from cppreference.com:

 char str[] = "now # is the time for all # good men to come to the # aid of their country";
 char delims[] = "#";
 char *result = NULL;
 result = strtok( str, delims );
 while( result != NULL ) {
     printf( "result is \"%s\"\n", result );
     result = strtok( NULL, delims );
 }
ChronoPositron
+3  A: 

So many people are suggesting strtok... Why? strtok is a left-over of stone age of programming and is good only for 20-line utilities!

Each call to strtok modifies strToken by inserting a null character after the token returned by that call. [...] [F]unction uses a static variable for parsing the string into tokens. [...] Interleaving calls to this function is highly likely to produce data corruption and inaccurate results.

scanf, as in Jerry Coffin's answer, is a much better alternative. Or, you can do it manually: find the separator with strchr, then copy parts to separate buffers.

atzz
A: 

Try using the following regex it will find anything with chars a-z A-Z followed by a ","

"[A-Z]," if you need lower case letter too try "[a-zA-Z],"

If you need it to search for the second part first you could try the following

",[0-9]{2}:[0-9]{2}:[0-9]{2}:[0-9]{2}:[0-9]{2}:[0-9]{2}"

There is an example on how to use REGEX's at http://ddj.com/184404797

Thanks, V$h3r

Vsh3r