/* strcmp takes in two character pointers (i.e. the addresses of two strings) and compares them. The function returns: 0 if the strings are the same. a negative number if s1 comes first in a lexicographical ordering - most compilers today return -1, but any negative gives same meaning a positive number if s2 comes first in a lexicographical ordering. - most compilers today return 1, but any positive gives same meaning A lexicographical ordering is similar to alphabetical order, but it goes by the character's assigned values (ascii values in most cases). For ascii valued characters, all uppercae letters come before all lowercase letters */ int strcmp(const char * s1, const char * s2) { int i = 0; while ((s1[i] != '\0') && (s1[i] == s2[i])) i++; // return the comparison of the first differing characters // (or the null characters, if strings are the same) return (s1[i] - s2[i]); /* Also, if an implementation just returns -1, 0, or 1, the * return portion could be the following: * * int result = s1[i] - s2[i]; * * if (result == 0) return 0; * else if (result > 0) return 1; * else return -1; * */ } int strcmp(const char * s1, const char * s2) { while ((*s1) && (*s1 == *s2)) { s1++; s2++; } // return the comparison of the first differing characters // (or the null characters, if strings are the same) return (*s1 - *s2); }