Thread: Storing Multiple Strings

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    44

    Storing Multiple Strings

    How would I store multiple strings that a user inputs? Also the user can enter as many strings as he wants until he enters an empty string ("str[0] == '\0').

    I am guessing I would want to store each string into a different array but since the amount of strings is always changing how would I store them that can adapt to the amount of strings entered.

    This is the code I have so far that simply loops everytime the user inputs a string.

    Code:
        int ch, count = 0;
        char *result;
    
        do
        {    
            int i = 0;
    
            str[0] = 0;
    
            printf ("\nInput string values: ");    
    
            while ((ch = getchar()) != '\n' && i < 81)
            {
                str[i++] = ch;
                str[i] = 0;
            }
            if (str[0] != '\0')
            {
                result = strtok(str, "\t  ");
    
                while (result != NULL)
                {
                    printf ("\nstring %i %s \n", count, result);
                    count++;
                    result = strtok(NULL, "\t ");
                }
            }
        }
        while (str[0] != '\0');
    Somewhere in that loop I need to store each of those strings and so I was wondering if there was a way to do that?
    Last edited by skmightymouse; 05-03-2012 at 08:07 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You want to store the individual tokens into an array?

    The basic idea is to use malloc to obtain a dynamic array of pointers to char. Then, once you obtain a token, you malloc memory for it, copy the contents over, and then store the pointer in the dynamic array of pointers to char. At some point, the size of the array in use reaches its capacity, upon which you use realloc to "expand" the dynamic array of pointers to char (typically, expansion is by some factor rather than a fixed amount, e.g., 1.5 or 2).

    When you are done, you free the memory for each token, then free the memory for the dynamic array of pointers to char.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    44
    Sorry, that is a lot to take in at once. I am just going to start by addressing the dynamic array of pointers to char using malloc.

    Would the code look similar to this: array = malloc(81 * sizeof( *array));
    Last edited by skmightymouse; 05-03-2012 at 08:54 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about storing strings.
    By Laythe in forum C Programming
    Replies: 8
    Last Post: 02-27-2011, 09:34 PM
  2. Storing Many Strings- Which STL Container?
    By bengreenwood in forum C++ Programming
    Replies: 7
    Last Post: 08-27-2009, 05:13 AM
  3. Storing multiple string in array
    By winsonlee in forum C Programming
    Replies: 2
    Last Post: 03-15-2004, 07:49 AM
  4. storing strings into a 2d array
    By ronin in forum C Programming
    Replies: 2
    Last Post: 10-24-2002, 09:36 PM
  5. reading and storing strings
    By Unregistered in forum C++ Programming
    Replies: 6
    Last Post: 02-01-2002, 12:38 PM