Thread: Sorting a list of entered words

  1. #1
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688

    Sorting a list of entered words

    Not one of my C books teaches me this, but can I ask... as I have to specify the array size and the length of the string, do I use a 2 dimensional array to input sort and output the strings?

    I assume I cannot use a single one as all I can do with that is speicfy the array size.

    I had a quick mess around with a one dimensional without the sort method and im already running into problems with a seg fault as I havent specified the size. I also find it incredible that my book expects me to know how to do this when not once did it mention sorting string-type arrays, only integer and float using bubble and selection sort.

    Code:
    #include <stdio.h>
    
    #define ARRAY_SIZE 3
    
    /*main function - begins program execution -----------------------------------*/
    int main ( void )
    {
       char names[ ARRAY_SIZE ];
       int i;
    
       printf("Enter three names: ");
    
       for ( i = 0; i < ARRAY_SIZE; i++ )
       {
          scanf("%30s", &names[ i ]);
       }
    
       printf("\n\nYou entered\n\n");
    
       for ( i = 0; i < ARRAY_SIZE; i++ )
       {
          printf("%c\n", names[ i ]);
       }
    
       getchar(); /*freeze console output window*/
    
       return 0; /*return value from int main*/
    }
    Double Helix STL

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    char names[] is a single string (char array), char names[][] is an array of strings (char arrays).

    And you have problems because you are trying to squeeze lots of characters into one character (or really a location at some point in the char array).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >do I use a 2 dimensional array to input sort and output the strings?
    I would think so. The only other option would be an array of pointers to char, in which case you'd have to use malloc() to allocate the size of each element:
    Code:
    char *names[ARRAY_SIZE];
    Now if the strings were known at compile time you could avoid malloc():
    Code:
    char *names[] = {"dog", "cat", "pig", "horse", "bird"};
    Last edited by swoopy; 08-07-2007 at 09:41 AM.

  4. #4
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Thanks guys appriciated
    Double Helix STL

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help in debugging this code
    By jaggesh in forum C Programming
    Replies: 4
    Last Post: 02-09-2008, 08:47 AM
  2. instantiated from here: errors...
    By advocation in forum C++ Programming
    Replies: 5
    Last Post: 03-27-2005, 09:01 AM
  3. New Theme
    By XSquared in forum A Brief History of Cprogramming.com
    Replies: 160
    Last Post: 04-01-2004, 08:00 PM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM