How can I perform a case-insensitive string comparison?
I want to do a case-insensitive string comparison. What would be the easiest way to achieve this? I have the code below that does 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;
}
+3
source to share
2 answers
If you can deviate from the standard a little C
, you can use strcasecmp()
. This is the POSIX API.
Otherwise, you always have the option to convert the strings to a specific case (UPPER or lower) and then perform a normal comparison using strcmp()
.
+4
source to share