Unlike some people, I prefer to catch null pointers as early as possible (through a program crash and debugger), so I avoid any checks for null pointers below. In other words, do not pass a null pointer to these functions.
The str_neq function determines if the strings are not equal, which seems to be the correct logic based on your code (return 0 / false if equal and -1 / true if not equal):
int str_neq (const char *s1, const char *s2) { while (*s1 != '\0' && *s1 == *s2) ++s1, ++s2; return -(*s1 != *s2); }
To ensure the same behavior as strcmp , a small change is needed in the expression that evaluates the return value:
int str_compare (const char *s1, const char *s2) { while (*s1 != '\0' && *s1 == *s2) ++s1, ++s2; return *s1 - *s2; }
Hope this helps! :-)
source share