#include int strcmp( char s1[], char s2[] ) { int r; /* ---------------------------------- '\0' marks the end of the string ! ---------------------------------- */ while ( *s1 != '\0' && *s2 != '\0' && (r = (*s1++ - *s2++)) == 0 ); /* ---------------------------------------------------------------- If we reach here, then 1. one or both of s1 and s2 are exhausted ! or 2. r != 0 ---------------------------------------------------------------- */ return (r != 0) ? r : (*s1 - *s2); } int main(int argc, char *arg[]) { char a[] = "Hell"; char b[] = "Hello"; char c[] = "Helloe"; printf("strcmp(%s, %s) = %d\n", b, b, strcmp(b, b) ); printf("strcmp(%s, %s) = %d\n", b, a, strcmp(b, a) ); printf("strcmp(%s, %s) = %d\n", b, c, strcmp(b, c) ); }