The world’s Largest Sharp Brain Virtual Experts Marketplace Just a click Away
Levels Tought:
Elementary,Middle School,High School,College,University,PHD
| Teaching Since: | May 2017 |
| Last Sign in: | 407 Weeks Ago, 6 Days Ago |
| Questions Answered: | 66690 |
| Tutorials Posted: | 66688 |
MCS,PHD
Argosy University/ Phoniex University/
Nov-2005 - Oct-2011
Professor
Phoniex University
Oct-2001 - Nov-2016
Suppose that you have the statements:
string str1 = "Hello";
string str2 = "Hi";
string str3 = "Air";
string str4 = "Bill";
string str5 = "Big";
The following expressions show how string relational expressions evaluate.
|
Expression |
Value /Explanation |
|
str1 < str2 |
true str1 = "Hello" and str2 = "Hi". The first characters of str1 and str2 are the same, but the second character "e" of str1 is less than the second character "i" of str2. Therefore, str1 < str2 is true. |
|
str1 > "Hen" |
false str1 = "Hello". The first two characters of str1 and "Hen" are the same, but the third character "l" of str1 is less than the third character "n" of "Hen". Therefore, str1 > "Hen" is false. |
|
str3 < "An" |
true str3 = "Air". The first characters of str3 and "An" are the same, but the second character "i" of "Air" is less than the second character "n" of "An". Therefore, str3 < "An" is true. |
|
str1 == "hello" |
false str1 = "Hello". The first character "H" of str1 is less than the first character "h" of "hello" because the ASCII value of "H" is 72, and the ASCII value of "h" is 104. Therefore, str1 == "hello" is false. |
|
str3 <= str4 |
true str3 = "Air" and str4 = "Bill". The first character "A" of str3 is less than the first character "B" of str4. Therefore, str3 <= str4 is true. |
|
str2 > str4 |
true str2 = "Hi" and str4 = "Bill". The first character "H" of str2 is greater than the first character "B" of str4. Therefore, str2 > str4 is true. |
If two strings of different lengths are compared and the character-by-character comparison is equal until it reaches the last character of the shorter string, the shorter string is evaluated as less than the larger string, as shown next.
|
Expression  |
Value/Explanation  |
|
str4 >= "Billy" |
false str4 = "Bill". It has four characters, and "Billy" has five characters. Therefore, str4 is the shorter string. All four characters of str4 are the same as the corresponding first four characters of "Billy", and "Billy" is the larger string. Therefore, str4 >= "Billy" is false. |
|
str5 <= "Bigger" |
true str5 = "Big". It has three characters, and "Bigger" has six characters. Therefore, str5 is the shorter string. All three characters of str5 are the same as the corresponding first three characters of "Bigger", and "Bigger" is the larger string. Therefore, str5 <= "Bigger" is true. |
The program Chapter4_StringComparisons.cpp at the Web site accompanying this book shows the results of the previous expressions.
Hel-----------lo -----------Sir-----------/Ma-----------dam-----------Tha-----------nk -----------You----------- fo-----------r u-----------sin-----------g o-----------ur -----------web-----------sit-----------e a-----------nd -----------acq-----------uis-----------iti-----------on -----------of -----------my -----------pos-----------ted----------- so-----------lut-----------ion-----------.Pl-----------eas-----------e p-----------ing----------- me----------- on-----------cha-----------t I----------- am----------- on-----------lin-----------e o-----------r i-----------nbo-----------x m-----------e a----------- me-----------ssa-----------ge -----------I w-----------ill----------- be-----------