![]() |
|
#1
|
|||
|
|||
string comparisonnote the following code...
#include <iostream> #inlcude <string> using namespace std; void main() { string x = "Hello"; string y = "Hey"; if( x < y ) cout << "x is less than y"; else cout << "x is either equal to or greater than y"; }How does the < operator work on strings? For example: for string x, does it add up the ASCII values of each letter in the word 'Hello' and compare that to the value of Hey? ( H+e+l+l+o < H+e+y ) <-- like that? Thanks, Austin ![]() |
|
#2
|
|||
|
|||
|
Quote:
No, that's numerology. C++ string operators work on ASCII character values. All relational operators work. It looks at each string (starting at the first character). Comparison is according to the ASCII value of the characters (A < B, B < C, etc.). If alll the characters are the same, the strings are equal. Otherwise, the first time that a character is encountered where they are not equal, the inequality values are set (< or >). If all characters are equal up to the end of one of the strings, but the strings have different lengths, the shorter one is less than the longer one. So: Hello < Hey Heylo < Heyloo Hey < hey OK? Dave |
|
#3
|
|||
|
|||
|
Quote:
Awesome! Thanks Dave, you've been a big help Austin |
Recent GIDBlog
US Elections and the ?Voter?s Responsibility? by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| AdSonar spider / bot useragent string | JdS | Advertising & Affiliates Forum | 1 | 10-Mar-2006 17:05 |
| Including Maps and strings?? | maddie | C++ Forum | 17 | 05-Jul-2004 07:25 |
| Problem putting variables cumulatively into a string, | warny_maelstrom | C Programming Language | 3 | 05-Feb-2004 10:29 |
| [function] AutoLink (converts URLS into links inside a string) | JdS | PHP Code Library | 0 | 26-Jan-2004 06:02 |
| storing a token pointer as a string | CoreLEx | C Programming Language | 1 | 07-Oct-2003 12:33 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The