By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,655 Members | 962 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,655 IT Pros & Developers. It's quick & easy.

Compare strings

P: n/a
al
To compare two strings, not just to see if they are "equal" ("abcd"="abcd")
but tell their alphabetical order:

string a, b;
a = "abcd";
b = "bbcd";

Can all the ways below to do such comparison?

1. a > b

2. a.compare(b))

3. strcmp(a, b)

4. max(a, b)

Which one you prefer and why?

Thanks!
Jul 22 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
"al" <al***@168.net> wrote in message
news:MC***********************@bgtnsc04-news.ops.worldnet.att.net...
To compare two strings, not just to see if they are "equal" ("abcd"="abcd") but tell their alphabetical order:

string a, b;
a = "abcd";
b = "bbcd";

Can all the ways below to do such comparison?

1. a > b
This is good. What could be simpler?

2. a.compare(b))
Reminds me of a Fortran II IF statement, and that isn't a glowing
recommendation. :)

3. strcmp(a, b)
I don't like this one because it won't work. However if you ammend it to

strcmp(a.c_str(), b.c_str())

I still don't like it. Using char * as a string type is a leftover from C.

4. max(a, b)
This is good too, if that's what you mean. It means something different than
#1 of course.

Which one you prefer and why?

Thanks!


--
Cy
http://home.rochester.rr.com/cyhome/
Jul 22 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.