Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Help with sorting strings in C++

Options
  • 08-03-2002 3:19pm
    #1
    Posts: 0


    Can anyone help me out with a programming problem?

    I have a struct:

    struct word
    {
    char letters[40];
    };

    which is to resemble a word, using a char array.
    The question is, if i had a large number of this structs stored in a struct array, how would I sort them and print them alphabetically?

    It's wrecking my head thinking about it, the feckin thing is awkward.

    And no, i'm not looking for someone to "do my homework", just a bit of help thanks.
    Any help would be great.

    Thanking you in advance.


Comments

  • Registered Users Posts: 2,660 ✭✭✭Baz_


    use whatever sort algorithm you know, and for the test case you can use strcmp().

    strcmp() takes two strings, and if they are equal returns 0, if string 1 is greater it returns a number greater than one, and if string 1 is less it returns a number less than one.

    the strings are compared by ascii value so be carefull as the string "ZZZZZZZ" will be less than "aaaaaaa" because capital letters come first in terms of ascii values.


  • Posts: 0 [Deleted User]


    And the strcmp() function takes char arrays as strings?
    Might sound like a stupid question, seen as C++ doesn't really have a built in string class but I just want to be clear.

    And also, do I have to include string.h ?

    Thanks for the help, Baz, I'm progressing with the program, adding helper functions here and there, the help is good to have.

    Thanks


  • Registered Users Posts: 2,660 ✭✭✭Baz_


    yeah you need string.h, and yes it takes char arrays, as essentially they are strings.

    No problem by the way.


  • Closed Accounts Posts: 9 TroutMan


    Hey hey -

    you could use qsort outta the standard library (implementation of quicksort, which is a good general purpose sort).

    say your array of words was defined

    const int num_words=20;
    struct word wordlist[num_words];

    you could probably sort it with this code (untested)

    /* Got to write a compare function for qsort */
    int compare( const void *arg1, const void *arg2 )
    {
    struct word *word1 = (struct word*)arg1;
    struct word *word2 = (struct word*)arg2;
    return strcmp( word1->letters, word1->letters);
    }

    qsort(&wordlist,num_words,sizeof(struct word),compare);

    Parameters to qsort are

    1) address of data to sort (&wordlist, tho' the & is not really needed)
    2) number of elements in array
    3) size of an array element (could have used 'sizeof(wordlist[0])' )
    4) comparison function. Function that compares two array elements, returns -1, 0, or 1 depending on relative order of arguments.

    If you're a dab hand with function pointers you could, with your current word structure, pass in strcmp to qsort instead of defining your own comparison function. But that would be a bad idea.


    HTH; post again if any problems. Oh, and you need to include <stdlib.h> for qsort, and <search.h> if using MS C++.

    TroutMan Ka


  • Posts: 0 [Deleted User]


    Great thanks, lot to take in, but I'm getting there.

    I'll let ye know if i come up against more problems, C++ is still relatively new, takes time to get used to the lack of "safety nets" like the ones in Java, but it's better for it.

    Thanks again, very helpful


  • Advertisement
  • Closed Accounts Posts: 7,346 ✭✭✭Rev Hellfire


    why dont u use the stl string template ?

    #include <string>

    std::string sMoo; // or use the namespace and remove std::


  • Registered Users Posts: 2,660 ✭✭✭Baz_


    I think hes meant to do it the old school way, thats why hes using structs instead of classes.

    C pwns.


Advertisement