1

I have a string that includes two names and a comma how can i take them apart nd write them to seperate strings.
Example

 char *line="John Smith,Jane Smith";    

I am thinking of using sscanf function.

 sscanf(line,"%s,%s",str1,str2);    

What should i do?
note: I can change comma to space character.

haccks
  • 104,019
  • 25
  • 176
  • 264
ossobuko
  • 851
  • 8
  • 25
  • 2
    You may want to give this a look: http://stackoverflow.com/questions/9210528/split-string-with-delimiters-in-c – wjmolina Jan 02 '14 at 18:04
  • 1
    Strtok() http://www.cplusplus.com/reference/cstring/strtok/ – Joe DF Jan 02 '14 at 18:04
  • @JoeDF If that family, then `strtok_r()` (it's not inherently thread-unsafe). –  Jan 02 '14 at 18:07
  • 1
    Don't use `strtok`, it modifies the string. As he's using a string-constant it will invoke undefined behaviour. – Kninnug Jan 02 '14 at 18:08

2 Answers2

8

I am thinking of using sscanf function.

Don't even think about it.

char line[] = "John Smith,Jane Smith";
char *comma = strchr(line, ',');
*comma = 0;
char *firstName = line;
char *secondName = comma + 1;
  • 1
    Actually there is a integer between names => "john Smith,10,Jane Smith". I tought i could manage it if i understand how to do it with only 2 strings. But your answer is a little complex for me (i'm absolute beginner) Do you mind at least a little explaining it to me? – ossobuko Jan 02 '14 at 18:14
  • 2
    @OssoBuko Yes. You search for the position of the comma with `strchr()`, then overwrite it with a 0 which terminates the first string. Then you have your first string. Then, you know that the second string starts immediately after the comma, so you grab a pointer to the character after it (`comma + 1`), and there you go, you got the second string too. –  Jan 02 '14 at 18:16
  • @OssoBuko; This a very simple answer if you have some idea about [`strchr`](http://www.tutorialspoint.com/ansi_c/c_strchr.htm). – haccks Jan 02 '14 at 18:58
  • Sorry mate. I thought i can accept multiple. And i didn't know it was wrong. Sorry to give you a headache. – ossobuko Jan 02 '14 at 19:24
-1

Here's how you could do it using strtok:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() 
{
    // Your string.
    char *line = "John Smith,10,Jane Smith";

    // Let's work with a copy of your string.
    char *line_copy = malloc(1 + strlen(line));
    strcpy(line_copy, line);

    // Get the first person.
    char *pointer = strtok(line_copy, ",");    
    char *first = malloc(1 + strlen(pointer));
    strcpy(first, pointer);

    // Skip the number.
    strtok(NULL, ",");

    // Get the second person.
    pointer = strtok(NULL, ",");    
    char *second = malloc(1 + strlen(pointer));
    strcpy(second, pointer);

    // Print.
    printf("%s\n%s", first, second);

    return 0;
}
wjmolina
  • 2,625
  • 2
  • 26
  • 35
  • 3
    **This is wrong** on so many levels. Firstly, there's an unnecessary copy. You could have just declared `line` as an array. Second, there's no memory allocated for `line_copy`, so `strcpy()`ing onto it leads to undefined behavior (meaning that this could crash or, what's even worse, it could fail silently, causing bigger, hard-to-detect problems elsewhere.) Also, it's better to use `strtok_r()` instead of `strtok()` because the latter is inherently thread-unsafe. –  Jan 02 '14 at 18:29
  • Okay, my code was not suited for industry use alright. I made some changes to it. But when Osso writes a multi-threaded program, I will use `strtok_r`; he said he's an *absolute beginner*. – wjmolina Jan 02 '14 at 18:51
  • 2
    @JosuéMolina And that's why it's dangerous to write incorrect code for him. –  Jan 02 '14 at 18:52
  • 1
    I feel the down-votes and delete request are unnecessarily harsh; my approach was conceptually correct. That it was *unsafe* at first did not deem it *wrong*. Thank you for the constructive criticism, though; I learned something new. – wjmolina Jan 02 '14 at 19:03
  • By the way, I already removed my downvote. But down voters should leave a comment. – haccks Jan 02 '14 at 19:12