strcmpi() and Case-Insensitive String Comparison in C

strcmpi() is a non-standard name used by some libraries for case-insensitive string comparison. Portable ISO C does not provide a direct case-insensitive equivalent, so a small comparison function can use tolower().

Portability: do not assume strcmpi() or POSIX strcasecmp() exists on every ISO C implementation.

Legacy Syntax Top ↑

strcmpi(string1, string2);  // non-standard

Portable Case-Insensitive Comparison Top ↑

#include <ctype.h>
#include <stdio.h>

int compare_ignore_case(const char *a, const char *b) {
    while (*a != '\0' && *b != '\0') {
        unsigned char ca = (unsigned char)*a;
        unsigned char cb = (unsigned char)*b;

        int lower_a = tolower(ca);
        int lower_b = tolower(cb);

        if (lower_a != lower_b) {
            return lower_a - lower_b;
        }

        a++;
        b++;
    }

    return (unsigned char)*a - (unsigned char)*b;
}

int main(void) {
    const char *first = "Plus2Net";
    const char *second = "plus2net";

    if (compare_ignore_case(first, second) == 0) {
        printf("Strings are equal ignoring case.\n");
    } else {
        printf("Strings are different.\n");
    }

    return 0;
}

Output

Strings are equal ignoring case.

As with strcmp(), use the result by sign: less than zero, zero, or greater than zero. Do not rely on exact values such as -1 or 1.

Compare User Input Top ↑

#include <ctype.h>
#include <stdio.h>
#include <string.h>

int compare_ignore_case(const char *a, const char *b) {
    while (*a != '\0' && *b != '\0') {
        int ca = tolower((unsigned char)*a);
        int cb = tolower((unsigned char)*b);

        if (ca != cb) {
            return ca - cb;
        }

        a++;
        b++;
    }

    return (unsigned char)*a - (unsigned char)*b;
}

int main(void) {
    char first[60];
    char second[60];

    printf("First string: ");
    if (fgets(first, sizeof first, stdin) == NULL) return 1;

    printf("Second string: ");
    if (fgets(second, sizeof second, stdin) == NULL) return 1;

    first[strcspn(first, "\n")] = '\0';
    second[strcspn(second, "\n")] = '\0';

    printf("%s\n",
        compare_ignore_case(first, second) == 0
            ? "Equal ignoring case"
            : "Different");

    return 0;
}

Preserved Comparison Variations Top ↑

Negative, Positive and Zero Comparison Results

Like strcmp(), a case-insensitive comparison should be interpreted by sign: negative, zero or positive. Do not depend on an exact value such as -1 or 1.

#include <ctype.h>
#include <stdio.h>

int compare_ignore_case(const char *a, const char *b) {
    while (*a != '\0' && *b != '\0') {
        int ca = tolower((unsigned char)*a);
        int cb = tolower((unsigned char)*b);

        if (ca != cb) {
            return ca - cb;
        }

        a++;
        b++;
    }

    return (unsigned char)*a - (unsigned char)*b;
}

int main(void) {
    printf("Plus2 vs plus2net: %d\n",
           compare_ignore_case("Plus2", "plus2net"));
    printf("plus2net vs Plus2: %d\n",
           compare_ignore_case("plus2net", "Plus2"));
    printf("plus2net vs Plus2net: %d\n",
           compare_ignore_case("plus2net", "Plus2net"));

    return 0;
}

POSIX strcasecmp() Alternative

On POSIX systems, strcasecmp() from <strings.h> provides case-insensitive comparison. It is not part of ISO C, so use the portable function above when portability is required.

#include <stdio.h>
#include <strings.h>

int main(void) {
    const char first[] = "Plus2Net";
    const char second[] = "plus2net";

    if (strcasecmp(first, second) == 0) {
        printf("The strings are equal ignoring case.\n");
    } else {
        printf("The strings are different.\n");
    }

    return 0;
}




plus2net.com






✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer