Implementation of strcmp

Uhm.. way too complicated. Go for this one:

int strCmp(const char* s1, const char* s2)
{
    while(*s1 && (*s1 == *s2))
    {
        s1++;
        s2++;
    }
    return *(const unsigned char*)s1 - *(const unsigned char*)s2;
}

It returns <0, 0 or >0 as expected

You can’t do it without pointers. In C, indexing an array is using pointers.

Maybe you want to avoid using the * operator? 🙂

Leave a Comment