hello everyone...I am just new in this forum.I am having problem in sorting C strings alphabetically...anyone please help!
hello everyone...I am just new in this forum.I am having problem in sorting C strings alphabetically...anyone please help!
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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.
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.
Correct again.
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.
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.
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.