Check if Two Strings are Equal Ignoring Case in C
In C, we can compare two strings while ignoring case using functions like strcasecmp(), stricmp() (for Windows), or by manually converting characters to lowercase or uppercase before comparison. Since string comparison in C is case-sensitive by default, we need to use special techniques to perform a case-insensitive comparison.
Examples of Case-Insensitive String Comparison
1. Using strcasecmp() (POSIX Standard) to Compare Strings Ignoring Case
In this example, we will use the strcasecmp() function to compare two strings in a case-insensitive manner. This function is available in POSIX systems like Linux and macOS.
main.c
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "Hello";
char str2[] = "hello";
// Using strcasecmp to compare strings ignoring case
if (strcasecmp(str1, str2) == 0) {
printf("Strings are equal (ignoring case)\n");
} else {
printf("Strings are not equal\n");
}
return 0;
}
Explanation:
- We declare two character arrays
str1andstr2initialized with different cases. - The
strcasecmp()function compares the strings in a case-insensitive way. - If the function returns
0, it means both strings are equal. - We use an
ifstatement to check the result and print the appropriate message.
Output:
Strings are equal (ignoring case)
2. Using stricmp() (Windows-Specific)
For Windows users, the function stricmp() provides similar case-insensitive string comparison functionality.
main.c
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "WORLD";
char str2[] = "world";
// Using stricmp (Windows only)
if (stricmp(str1, str2) == 0) {
printf("Strings are equal (ignoring case)\n");
} else {
printf("Strings are not equal\n");
}
return 0;
}
Explanation:
- We declare two strings
str1andstr2with different cases. - The
stricmp()function compares the strings case-insensitively. - A return value of
0means the strings are equal. - We use an
ifcondition to determine and print the result.
Output:
Strings are equal (ignoring case)
3. Comparing Strings by Converting to Lowercase
If strcasecmp() or stricmp() is not available, we can manually convert both strings to lowercase before comparison.
main.c
#include <stdio.h>
#include <ctype.h>
#include <string.h>
void toLowerCase(char *str) {
for (int i = 0; str[i]; i++) {
str[i] = tolower(str[i]);
}
}
int main() {
char str1[] = "Programming";
char str2[] = "PROGRAMMING";
// Convert both strings to lowercase
toLowerCase(str1);
toLowerCase(str2);
// Compare strings
if (strcmp(str1, str2) == 0) {
printf("Strings are equal (ignoring case)\n");
} else {
printf("Strings are not equal\n");
}
return 0;
}
Explanation:
- We create a helper function
toLowerCase()that converts a string to lowercase usingtolower(). - We call this function for both
str1andstr2before comparing them. - We use
strcmp()to compare the modified strings. - If the result is
0, the strings are equal, otherwise, they are different.
Output:
Strings are equal (ignoring case)
Conclusion
In this tutorial, we explored different ways to check if two strings are equal while ignoring case in C:
- Using
strcasecmp()(for POSIX systems). - Using
stricmp()(for Windows). - Manually converting both strings to lowercase before comparison.
