Friday, July 31, 2009

How do you compare strings in c++?

I have no Idea how to make sense of compraing strings in c++





for example, a statement such as -(someString1 %26gt; someString2)





how does this make any sense?





how can letters be greater than or less than each other?





please help.

How do you compare strings in c++?
1. "how can letters be greater than or less than each other?"





a: Each character has a numeric value. 'C' has a numerically greater value than 'B'. Thus, this is a valid expression, and prints "1":


cout %26lt;%26lt; ('C' - 'B') %26lt;%26lt; endl ;





...and this (should) print "67"


cout %26lt;%26lt; 'C' %26lt;%26lt; endl;





2. "for example, a statement such as -(someString1 %26gt; someString2) how does this make any sense"





a: since a string is a sequence of characters, and each character has a numeric value, then a string has a numeric value. And thus one string may have a greater numeric value than another. This fact is exploited in string sorting algorithms. It's how Windows Explorer, for example, sorts your directory entries and makes them all neat for you.





a statement such as


if(someString1 %26gt; someString2)


....





won't do what you expect [ie: compare the strings] unless you overload the "%26gt;" operator. Generally, we'd overload it with the strcmp() function in a class implementation like so:





boolean string::operator %26gt; (const string %26amp;str)


{


return strcmp(data, str.data) %26gt; 0;


}
Reply:No, actually in C++ you can use the == operator to directly compare because it's been operator overloaded. It does *not* compare the memory addresses as what would happen in C. I could be wrong, but I'm sure C++ also overloads the %26lt; and %26gt; operators that conform to lexicographical order (which is comparing each letter until they differ or one ends and tests the value of the characters to determine whether a string is larger than the other).
Reply:well the best I would say is would be to say str1 == str2 for equality in strings, but as for saying is one greater then another I don't think there is a way simply because as far as I can think of it would have no real use. now you can compare lengths( in java its (name).length() ), but being a heavy user of Java I forget what the command is.
Reply:In C++ strings are pointers to characters, so statements like "string1 == string2" or "string1 %26gt; string2" will simply compare the *pointer* values, not the actual strings that they point to.





To compare strings in C++ use strcmp(). If you check the documentation you'll find there are several variants of this function e.g. for case insensitive compares, comparing only a set number of characters etc.
Reply:First realize that a string is stored internally as a series of numeric values (ASCII, ANSI,UNICODE, etc) and that text is merely an output-time representation. So in a binary sense it's a completely rational operation.





But typically, when strings are compared as one being greater or less than another, the op infers alphabetic order, 'and' %26lt; 'army'; 'dog' %26gt; 'cat', etc. One word is greater than another because it comes after the other in a dictionary.





If the comparison is case-insensitive, then 'Dog' %26gt; 'cat' is still true, but if it is case-sensitive, then 'aardvark' %26gt; 'Zoophilia' is true, because the ASCII values of all upper-case letters are lower than that of lower-case 'a'..


No comments:

Post a Comment