Thread: how to sort strings alphabetically?

  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
    28,413
    For starters, take a look at qsort() from <stdlib.h> and strcmp() from <string.h>.
    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
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    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,834
    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,834
    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, 04:03 AM
  3. Replies: 2
    Last Post: 05-06-2003, 08:34 AM
  4. bubble sort with strings
    By volk in forum C++ Programming
    Replies: 2
    Last Post: 05-04-2003, 03:46 PM
  5. Radix Sort, Strings, and Linked Lists
    By dark paladin in forum C++ Programming
    Replies: 2
    Last Post: 04-24-2003, 03:24 PM