cstrstr

Case insensitive version of strstr()


In order to solve a problem from K&R C book, I am trying to write a function strstrci() which is the case insensitive version of strstr()

char * strstrci (char *s, char *p)
{
    int i, j;
    for (i = 0; *(s + i) != '\0'; i++)
    {
        if (*(s + i) == *p || *(s + i) == *p + 32 || *(s + i) == *p - 32)
        {
            for (j = 1; *(s + i + j) == *(p + j) || *(s + i + j) == *(p + j) + 32 || *(s + i + j) == *(p + j) - 32; j++)
            {
                if (*(p + j) == '\0')
                    return (s + i);
            }
        }
    }
    return NULL;
}

The function seems to work well for all cases except when the pattern is the last part of the input string. For example: string : On a wall he sat pattern: sat In such a case it returns a NULL. Please point out the mistake


Solution

  • There are multiple problems:

    Here is a modified version using the standard header <ctype.h>:

    #include <ctype.h>
    
    char *strstrci(const char *s, const char *p) {
        if (*p == '\0')
            return (char *)s;
        for (; *s; s++) {
            if (tolower((unsigned char)*s) == tolower((unsigned char)*p)) {
                size_t i;
                for (i = 1;; i++) {
                    if (p[i] == '\0')
                        return (char *)s;
                    if (tolower((unsigned char)s[i]) != tolower((unsigned char)p[i]))
                        break;
                }
            }
        }
        return NULL;
    }