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

</>
Copy
#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:

  1. We declare two character arrays str1 and str2 initialized with different cases.
  2. The strcasecmp() function compares the strings in a case-insensitive way.
  3. If the function returns 0, it means both strings are equal.
  4. We use an if statement 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

</>
Copy
#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:

  1. We declare two strings str1 and str2 with different cases.
  2. The stricmp() function compares the strings case-insensitively.
  3. A return value of 0 means the strings are equal.
  4. We use an if condition 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

</>
Copy
#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:

  1. We create a helper function toLowerCase() that converts a string to lowercase using tolower().
  2. We call this function for both str1 and str2 before comparing them.
  3. We use strcmp() to compare the modified strings.
  4. 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:

  1. Using strcasecmp() (for POSIX systems).
  2. Using stricmp() (for Windows).
  3. Manually converting both strings to lowercase before comparison.