Is there a stardard C function that compares two strings of C and returns a pointer to the first character that is different, and NULL if the strings are equal?
#include <stdio.h>
int main()
{
const char *p, *s1 = "foobar", *s2 = "foobaz";
p = strdiffchr(s1, s2);
if (!p)
puts("the strings are equal");
else
printf("s1 has %c, where s2 has %c\n", *p, s2[p - s1]);
return 0;
}
EDIT: I chose the following solution:
char *strdiffchr(const char *s1, const char *s2)
{
while (*s1 && *s1 == *s2) {
s1++;
s2++;
}
return (*s1 == *s2) ? NULL : (char *)s1;
}
source
share