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
  • 133,132
  • 16
  • 183
  • 261
Jon Erickson
  • 112,242
  • 44
  • 136
  • 174

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
  • 133,132
  • 16
  • 183
  • 261
2

You can use strcmpi() function.

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

only for Windows systems.

Sourav Kanta
  • 2,727
  • 1
  • 18
  • 29