A slightly more cumbersome way to avoid atoifriends is to prefix the shorter of two lines with zeros. Or, assuming there are no null prefixes, just compare the length first (since shorter lines should have a smaller value) and then do a lexicographic comparison:
int numerical_strcmp(const char* a, const char* b) {
return (strlen(a) != strlen(b)) ? ((strlen(a) > strlen(b)) ? 1 : -1) : strcmp(a, b);
}
Oh, and that requires strings to contain non-negative numbers.
source
share