6

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.

1
  • Its not clear what kind of syntax checking do you need. Are you only trying to do CSV? Do the first characters have to be uppercase? Commented Apr 22, 2010 at 23:37

6 Answers 6

7

One possibility is something like this:

char first[20], second[20];

scanf("%19[^,], %19[^\n]", first, second);
Sign up to request clarification or add additional context in comments.

1 Comment

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 :)
3

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.

Comments

3
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.

5 Comments

i get segmentation fault when trying this out? what may be the reason?
@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).
@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?
@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.
Your second line can actually be char *second = strtok(NULL, ""); to fetch the remainder of the string.
1

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

Comments

1

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 );
 }

Comments

-1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.