GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 21-Mar-2004, 20:26
soccer022483 soccer022483 is offline
New Member
 
Join Date: Mar 2004
Posts: 17
soccer022483 is on a distinguished road
Exclamation

string comparison


note 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  
Old 21-Mar-2004, 20:55
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,305
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by soccer022483
note the following code...


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

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  
Old 21-Mar-2004, 21:13
soccer022483 soccer022483 is offline
New Member
 
Join Date: Mar 2004
Posts: 17
soccer022483 is on a distinguished road
Quote:
Originally Posted by davekw7x
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

Awesome! Thanks Dave, you've been a big help

Austin
 
 

Recent GIDBlogProblems with the Navy (Officers) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
AdSonar spider / bot useragent string JdS Advertising & Affiliates Forum 1 10-Mar-2006 16:05
Including Maps and strings?? maddie C++ Forum 17 05-Jul-2004 06:25
Problem putting variables cumulatively into a string, warny_maelstrom C Programming Language 3 05-Feb-2004 09:29
[function] AutoLink (converts URLS into links inside a string) JdS PHP Code Library 0 26-Jan-2004 05:02
storing a token pointer as a string CoreLEx C Programming Language 1 07-Oct-2003 11:33

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 16:41.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.