Sorting an array alphabetically?

This is a discussion on Sorting an array alphabetically? within the C Programming forums, part of the General Programming Boards category; I am using structures of arrays, and one of the arrays holds football teams. I want to organise the data ...

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    22

    Sorting an array alphabetically?

    I am using structures of arrays, and one of the arrays holds football teams. I want to organise the data alphabetically, and have it displayed. Can this be done?

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    Hmmm. So if I am correct you have a structure with arrays as members and one of the arrays contains the names of football teams. And you want to organise that array alphabetically. Yes, that is possible, you could sort the array and then print it out. You can use qsort, part of the standard library, to sort the array.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    2

    sorting line alphabetically in C

    Hi I am very new to C, I need help with:
    -prompting the user for a string of up to 10 characters.
    -the output should be all alphabetic characters from string converted to lower case and sorted in ascending order.

    #define MAX_STRING 10
    #define EXTRA_CHARS 2


    printf( "Sort Line\n");
    printf("%s", "-------\n");
    printf ("Please enter a string (1-10 characters):\n");
    sscanf("%s", &a);
    char a[MAX_STRING];
    fgets(a, MAX_STRING+EXTRA_CHARS, stdin);

    SORT_LINE;

    for (i >= a; i<= z; i++);

    {
    Last edited by beginner to c; 03-08-2011 at 07:54 PM.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Edit your post and add code tags.

    Why is 'MAX_STRING' 10, but you say "enter 1 - 40 characters"?


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

  5. #5
    THANK YOU KINDLY SIR Phenax's Avatar
    Join Date
    Mar 2011
    Posts
    74
    Thing's you'll probably need:
    tolower function - will convert an upper-case character to lower-case. So you'll probably need to loop through your string and call the tolower function on each character.

    A sorting algorithm - try Bubble sort - Wikipedia, the free encyclopedia

    Remember - a string in C is just an array of characters.
    Quote Originally Posted by Plato
    Never discourage anyone...who continually makes progress, no matter how slow.

  6. #6
    Registered User
    Join Date
    Feb 2011
    Posts
    12
    void sortData(char* string[MAX])
    {
    ///////////////////////////LOCAL DECLARATIONS/////////

    int current; int walker;
    int smallestIndex;
    char* temp;

    ///////////////////////////DEFINITIONS//////////////////

    for (current = 0; current <MAX - 1; current++)
    {
    smallestIndex = current;
    for (walker = current; walker < MAX; walker ++)
    {
    if (strcmp(string[walker], string[smallestIndex]) < 0)
    smallestIndex = walker;
    }//for walker

    //Swap to position smallest at what is the current position
    temp = string[current];
    string[current] = string[smallestIndex];
    string[smallestIndex] = temp;
    }//for current
    return;
    }

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,261
    Start your own thread instead of hijacking another persons, especially considering that it is over nine years old!
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    Registered User
    Join Date
    Mar 2011
    Posts
    2

    how people are rude in here

    I said that I am new and had no idea of taking any body's thread or bothering anyone!
    please forgive me and I will never use this website with such weird educated people!

    good luck

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. Sorting an array
    By OnionKnight in forum C++ Programming
    Replies: 4
    Last Post: 04-24-2005, 03:31 AM
  3. Array of Structures: Sorting
    By Drainy in forum C Programming
    Replies: 3
    Last Post: 04-13-2005, 09:55 AM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  5. sorting array efficiently...can't find the error
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 04-09-2002, 02:32 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21