/* Programmed by Kurt Nørmark, April 2003 */ #include #include /* Assume as a precondition that both s1 and s2 are '0' terminated strings */ int mystrcmp(const char *s1, const char *s2){ char *s1_ptr, *s2_ptr; int result, done; s1_ptr = s1; s2_ptr = s2; do { done = 1; /* done in this iteration unless we change our mind */ if (*s1_ptr == '\0' && *s2_ptr == '\0') result = 0; else if (*s1_ptr == '\0' && *s2_ptr != '\0') result = -1; else if (*s1_ptr != '\0' && *s2_ptr == '\0') result = 1; else if (*s1_ptr < *s2_ptr) result = -1; else if (*s1_ptr > *s2_ptr) result = 1; else{ s1_ptr++; s2_ptr++; done = 0; } } while (!done); return result; } void compare(char *s1, char *s2){ printf("strcmp(\"%s\",\"%s\") = %i, %i\n", s1, s2, mystrcmp(s1,s2), strcmp(s1,s2)); } int main(void) { compare("abe", "kat"); compare("abe", "abe"); compare("", "kat"); compare("", ""); compare("", "a"); compare("a", ""); compare("abe", "aben"); compare("aben", "aben"); return 0; }