Compare strings



 DEVELOP > c-Plus-Plus > Compare strings

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "al"
Date: 20 Dec 2003 07:23:56 PM
Object: Compare strings
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!
.

User: "Cy Edmunds"

Title: Re: Compare strings 20 Dec 2003 08:16:46 PM
"al" <allin@168.net> wrote in message
news:MC6Fb.502440$0v4.21709853@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/
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER