The code is:
#include <iostream>
int main() {
const char *str1{"Jill"};
const char *str2{"Jacko"};
int result{std::strcmp(str1, str2)};
if(result < 0) {
std::cout << str1 << " is less than " << str2 << '.' << std::endl;
} else if(result == 0) {
std::cout << str1 << " is equal to " << str2 << '.' << std::endl;
} else {
std::cout << str1 << " is greater than " << str2 << '.' << std::endl;
}
return 0;
}
Output: Jill is greater than Jacko.
My newbie question is: how come it returns the else statement block affirming Jill is greater than Jacko? What method is used in strcmp to compare those strings? is it by total, pair of bytes, whatever characters?
First 'J' is compared to 'J', they are equal. Then 'i' is compared to 'a', 'i' > 'a'
Therefor,
Jill is greater than Jacko