Strings, by default, can be compared with each other since they have operators to allow for it. That should make things a bit easier already.
For instance, the following program will print
"hello" is less than "world" |
, because the first letter of 'string_one' ('h') has a numerical, decimal value of 104, and the first character of 'string_two' ('w') has a numerical, decimal value of 119. 104 is less than 119.
You can find a list these values by searching for "ASCII table" or here:
http://www.asciitable.com/
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
#include <iostream>
#include <string>
int main() {
std::string string_one = "hello";
std::string string_two = "world";
if (string_one < string_two) {
std::cout << "\"" << string_one << "\" is less than \"" << string_two << "\"" << std::endl;
}
return 0;
}
|
Note, that upper- and lower-case letters have different values. In the ASCII table, the upper-case characters appear before the lower-case ones, which means a string such as "ABC" will be less than "abc", or even "aBC" or "abC". The way the comparison works is effectively by comparing the first characters of both strings, then the second characters of both strings, then the third, and so on.