how to sort strings alphabetically?

This is a discussion on how to sort strings alphabetically? within the C Programming forums, part of the General Programming Boards category; hello everyone...I am just new in this forum.I am having problem in sorting C strings alphabetically...anyone please help!...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    1

    how to sort strings alphabetically?

    hello everyone...I am just new in this forum.I am having problem in sorting C strings alphabetically...anyone please help!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,269
    For starters, take a look at qsort() from <stdlib.h> and strcmp() from <string.h>.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    Well fundamentally when you compare two strings using strcmp(st1, st2), it returns -1, 0 or 1 depending on whether st1 is less, equal, or greater than st2 respectively. Whatever sort algorithm you implement, it will probably be based on comparing two elements at a time.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by nonoob View Post
    Well fundamentally when you compare two strings using strcmp(st1, st2), it returns -1, 0 or 1 depending on whether st1 is less, equal, or greater than st2 respectively. Whatever sort algorithm you implement, it will probably be based on comparing two elements at a time.
    Technically, it doesn't have to be -1, 0 or 1, but rather "a negative, zero or a positive" value. Often, strcmp() returns (*st1 - *st2), which of course will not usually be -1 or 1 if st1 and st2 aren't equal.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    Correct again.

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    This is for nonoob. Though I do believe he is a linux guy, in which case he doesn't get a string comparison function that returns -1, 0, or 1 as its only return values.

  7. #7
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    First of all, that one is int CompareString(), which requires who-knows-what header and linking headaches. No thanks. I stick with things that work and would write my own if I had to.

    Then is says this curious thing:

    To maintain the C run-time convention of comparing strings, the value 2 can be subtracted from a nonzero return value. Then, the meaning of < 0, ==0 and > 0 is consistent with the C run times.

    Um. does that mean that natively it returns

    3 - if the result indicates a > b
    1 - if the result indicates a < b
    0 - if the result indicates a == b

    That way, when you subtract 2 from the non-zero results, you get 1 and -1 respectively.

  8. #8
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    If you include windows.h you can have that function. I thought those values were -1,0, and 1. My bad. I just use the macros that the docs say to use when comparing. It is a windows specific thing and I thought I would mention it for the sake of personal growth. Or whatever you want to call it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How do I heap sort alphabetically?
    By arih56 in forum C++ Programming
    Replies: 7
    Last Post: 12-12-2007, 01:00 AM
  2. threaded merge sort
    By AusTex in forum Linux Programming
    Replies: 4
    Last Post: 05-04-2005, 05:03 AM
  3. Replies: 2
    Last Post: 05-06-2003, 09:34 AM
  4. bubble sort with strings
    By volk in forum C++ Programming
    Replies: 2
    Last Post: 05-04-2003, 04:46 PM
  5. Radix Sort, Strings, and Linked Lists
    By dark paladin in forum C++ Programming
    Replies: 2
    Last Post: 04-24-2003, 04:24 PM

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