If you use C's strcmp() function it will return an integer > 0 if string1 is lexicographically larger than string2. Example: "Z" is greater than "A" because A comes first in the English alphabet and Z comes last. Example2: "A" less than "Z" so strcmp() will return some number < 0, and third example "A" == "A" so strcmp() will return 0.
This is the C++ forum, so why would you use strcmp?
You can use string objects. Include <string> and you can compare 2 string objects lexigraphically with the string.compare(string) method.
I'm not near a c++ compiler atm, but the implementation will looks something like this-
using namespace std;
string a ("John");
string b ("Mark");
int result = a.compare(b);
cout << "result of comparison between John and Mark is: " << result << "\n";
Edit: This isn't to trash on AD's post. C is very useful, and if performance isn't an issue then consider using the char-array--encapsulating class string.
Well, yeah, you can look at the first character, but that will not distinguish "Adam" from "Anthony". If the first characters are equal, then it's necessary to look at the second .... and then third, and so on - which is exactly what strcmp() does.
As a rough rule, in most character sets (at least, as far as English speakers are concerned);
- numbers are less than letters
- Uppercase letters are less than lower case ("Adam" comes before "adam", unless a case-insensitive comparison is performed with stricmp()).
The relative order of punctuation, whitespace, and graphical characters varies with character set, but normally the default character set makes sense to the locale (eg an English speaker will tend to use a character set that places whitespace less than common punctuation which is less than letters or numbers).