Thread: How to split a string on the comma to get an array with 3 strings using C Programming

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    7

    How to split a string on the comma to get an array with 3 strings using C Programming

    Hi, I need your help in solving my problem. This is what I have to do:

    Take for example, I have this string: David Lai, 91234567, Dav

    I need to split the commas and store "David Lai" "91234567" and "Dav" into an array and then store each part into variables - name, telephone and nickname.

    Below is my current code but the program always stopped responding after displaying "David Lai", indicating that there's something wrong with the codes.
    ---------------------------------------------------------------------------------------------------
    Code:
    int i;
    int count = 0;
    char str[] = "David Lai, 91234567, Dav";
    char delims[] = ",";
    char *result = NULL;
    char *store = NULL;
    char *name = NULL;
    char *telephone = NULL;
    char *nickname = NULL;
    char *strtok( char *strl, const char *str2);
    result = strtok( str, delims);
    
    while (results != NULL) {
        count++;
        printf("%s\n", result);
        
        for (i = 0; i < count; i++) {
            store[i-1] = result;
            printf ("array is %s\n", store[i]);
        }
    
        result = strtok( NULL, delims );
    
    } //end while
    ----------------------------------------------------------------------------------------------------

    Thank you for your help. =D

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Well store doesn't point to anything, and
    store[i-1] = result;
    indexes -1 on the first pass through your loop.

    char *store[3];
    would allow you to point to the 3 string fragments your code generates.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why are you prototyping strtok instead of including string.h?


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Jul 2010
    Posts
    20

    Lightbulb

    Try this (includes a minor optimization in execution speed):

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    int main() {
         int i;
         int count = 0;
         char str[] = "David Lai, 91234567, Dav, value1, value2, value3, value4, value5";
         const char delims[] = ",";
         char *result = NULL;
         char **store = NULL;
         char **tmp = NULL;
         char *name = NULL;
         char *telephone = NULL;
         char *nickname = NULL;
         result = strtok(str, delims);
         if (result != NULL) {
              store = malloc((count + 1) * sizeof(char *));
              store[count] = result;
              count++;
              tmp = malloc(count * sizeof(char *));
              printf("%s\n", result);
              for (i=0; i<count; i++) {
                   printf("array is %s\n", store[i]);
              }
              result = strtok(NULL, delims);
         }
         while (result != NULL) {
              free(tmp);
              tmp = malloc(count * sizeof(char *));
              for (i=0; i<count; i++) {
                   tmp[i] = store[i];
              }
              free(store);
              store = malloc((count + 1) * sizeof(char *));
              for (i=0; i<count; i++) {
                   store[i] = tmp[i];
              }
              store[count] = result;
              count++;
              printf("%s\n", result);
              for (i=0; i<count; i++) {
                   printf("array is %s\n", store[i]);
              }
              result = strtok(NULL, delims);
         }
         free(tmp);
         free(store);
         return 0;
    }
    Last edited by 68656c70; 07-24-2010 at 07:08 AM.

  5. #5
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Someone needs to stop showing *off*.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  3. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM