3

I want to do a case-insensitive string comparison. What would be the easiest way to accomplish that? I have the below code which performs a case-sensitive operation.

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

int main(int argc, char *argv[])
{
    char *str1 = "String";
    char *str2 = "STRING";

    if (strncmp(str1, str2, 100) != 0)
    {
        printf("=(");
        exit(EXIT_FAILURE);
    }

    return 0;
}
Sourav Ghosh
  • 130,437
  • 16
  • 177
  • 247
Jon Erickson
  • 107,872
  • 44
  • 134
  • 170

2 Answers2

4

If you can afford deviating a little from strict C standard, you can make use of strcasecmp(). It is a POSIX API.

Otherwise, you always have the option to convert the strings to a certain case (UPPER or lower) and then perform the normal comparison using strcmp().

Sourav Ghosh
  • 130,437
  • 16
  • 177
  • 247
2

You can use strcmpi() function.

if(strcmpi(str1,str2)!=0)

only for Windows systems.

Sourav Kanta
  • 2,697
  • 1
  • 16
  • 29