How to case insensitive string compare in C

1 Answer

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

int strcmp_i(char const* s1, char const* s2) {
    for (;; s1++, s2++) {
        int rv = tolower((unsigned char)*s1) - tolower((unsigned char)*s2);
        if (rv != 0 || !*s1)
            return rv;
    }
}

int main()
{
    char str1[] = "C Programming";
    char str2[] = "c programming";

    if (strcmp_i(str1, str2) == 0) {
        puts("equal");
    }
    else {
        puts("not equal");
    }
}




/*
run:

equal

*/

 



answered Sep 27, 2022 by avibootz

Related questions

2 answers 211 views
1 answer 160 views
2 answers 223 views
223 views asked Mar 31, 2017 by avibootz
1 answer 225 views
1 answer 150 views
1 answer 180 views
...