Compare Two Strings in C

In C, we can compare two strings using the strcmp() function from the string.h library or by manually comparing characters using loops. These methods help determine whether two strings are equal, or which one is lexicographically greater.

String Comparison is commonly used in various applications like user input validation, searching, and sorting strings.

In this tutorial, we will explore different ways to compare two strings with practical examples.


Examples of Comparing Two Strings in C

1. Comparing Two Strings Using strcmp()

In this example, we will use the strcmp() function from string.h to compare two strings and check if they are equal or different.

main.c

</>
Copy
#include <stdio.h>
#include <string.h>

int main() {
    char str1[] = "Hello";
    char str2[] = "Hello";

    // Using strcmp() to compare strings
    if (strcmp(str1, str2) == 0) {
        printf("Strings are equal.\n");
    } else {
        printf("Strings are not equal.\n");
    }

    return 0;
}

Explanation:

  1. We declare two character arrays, str1 and str2, and initialize them with the string "Hello".
  2. We use the strcmp() function, which compares str1 and str2 character by character.
  3. If the function returns 0, it means both strings are identical.
  4. If it returns a nonzero value, the strings are different.

Output:

Strings are equal.

2. Comparing Two Strings Without strcmp()

In this example, we will compare two strings manually using a loop, without using the strcmp() function.

main.c

</>
Copy
#include <stdio.h>

int main() {
    char str1[] = "Hello";
    char str2[] = "World";
    int i = 0, isEqual = 1;

    // Manually comparing each character
    while (str1[i] != '\0' || str2[i] != '\0') {
        if (str1[i] != str2[i]) {
            isEqual = 0;
            break;
        }
        i++;
    }

    if (isEqual) {
        printf("Strings are equal.\n");
    } else {
        printf("Strings are not equal.\n");
    }

    return 0;
}

Explanation:

  1. We declare two character arrays, str1 and str2, and assign them different values.
  2. We initialize a loop to traverse both strings character by character.
  3. We compare each character; if they are different, we set isEqual to 0 and exit the loop.
  4. If all characters match, isEqual remains 1 and we print “Strings are equal”. Otherwise, we print “Strings are not equal”.

Output:

Strings are not equal.

3. Case-Insensitive String Comparison Using strcasecmp()

In this example, we will compare two strings without considering case differences using the strcasecmp() function.

main.c

</>
Copy
#include <stdio.h>
#include <string.h>

int main() {
    char str1[] = "hello";
    char str2[] = "HELLO";

    // Using strcasecmp() for case-insensitive comparison
    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, with different cases.
  2. We use the strcasecmp() function, which performs a case-insensitive string comparison.
  3. If the function returns 0, the strings are identical, ignoring case.
  4. If the function returns a nonzero value, they are different.

Output:

Strings are equal (ignoring case).

Conclusion

In this tutorial, we explored different ways to compare two strings in C:

  1. Using strcmp(): Standard method for comparing two strings.
  2. Manual Comparison: Useful when avoiding built-in functions.
  3. Using strcasecmp(): For case-insensitive comparison.