Programming Example - strcmp

/*
 * return 0 iff s1 is the same string as s2
 * return < 0 iff s1 is lexicographically less than s2
 * return > 0 otherwise
 */
int
strcmp(char *s1, char *s2) {
    int i;
    
    for (i = 0; s1[i] == s2[i]; i++)
        if (s1[i] == '\0')
            return 0;
    return s1[i] - s2[i];
}

Index