469,282 Members | 1,751 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,282 developers. It's quick & easy.

Compare strings

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
1 2332
"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.

Similar topics

2 posts views Thread by J.W. | last post: by
3 posts views Thread by Drew | last post: by
19 posts views Thread by David zhu | last post: by
14 posts views Thread by Samuel R. Neff | last post: by
11 posts views Thread by balakrishnan.dinesh | last post: by
3 posts views Thread by Twinkle | last post: by
50 posts views Thread by titan nyquist | last post: by
2 posts views Thread by Peter Proost | last post: by
11 posts views Thread by Tony | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.