Check if Two Strings Are Equal in C

In C, we can compare two strings to check if they are equal using functions like strcmp() from the string.h library or by manually comparing each character using a loop.

In this tutorial, we will explore multiple ways to check if two strings are equal in C with detailed explanations and examples.


Examples to Compare Two Strings

1. Using strcmp() Function to Check if Strings Are Equal

In this example, we will use the strcmp() function from the string.h library to compare two strings.

The strcmp() function returns:

  • 0 if the strings are equal
  • A positive value if the first string is greater than the second
  • A negative value if the first string is less than the second

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("The strings are equal.\n");
    } else {
        printf("The strings are not equal.\n");
    }

    return 0;
}

Explanation:

  1. We declare two character arrays str1 and str2 with the same value “Hello”.
  2. We use the strcmp() function to compare str1 and str2.
  3. Since both strings are identical, strcmp() returns 0.
  4. We check if the return value is 0 and print “The strings are equal.”

Output:

The strings are equal.

2. Comparing Strings Character by Character

In this example, we will manually compare each character of the strings using a loop to check if they are equal.

main.c

</>
Copy
#include <stdio.h>

int areStringsEqual(char str1[], char str2[]) {
    int i = 0;
    
    while (str1[i] != '\0' && str2[i] != '\0') {
        if (str1[i] != str2[i]) {
            return 0; // Strings are not equal
        }
        i++;
    }
    
    return (str1[i] == '\0' && str2[i] == '\0'); // Both should end together
}

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

    // Manually comparing strings
    if (areStringsEqual(str1, str2)) {
        printf("The strings are equal.\n");
    } else {
        printf("The strings are not equal.\n");
    }

    return 0;
}

Explanation:

  1. We define a function areStringsEqual() that takes two strings as arguments.
  2. We use a loop to iterate through each character of both strings.
  3. If any character is different, we return 0 (strings are not equal).
  4. If we reach the end of both strings simultaneously, we return 1 (strings are equal).
  5. In the main() function, we call areStringsEqual() to compare str1 and str2.

Output:

The strings are equal.

3. Using strncmp() for Limited Comparison

In this example, we will use strncmp() to compare only the first n characters of two strings.

main.c

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

int main() {
    char str1[] = "Programming";
    char str2[] = "Program";
    
    // Comparing first 7 characters
    if (strncmp(str1, str2, 7) == 0) {
        printf("The first 7 characters are equal.\n");
    } else {
        printf("The first 7 characters are not equal.\n");
    }

    return 0;
}

Explanation:

  1. We declare two strings str1 and str2 with different lengths.
  2. We use strncmp() to compare only the first 7 characters of both strings.
  3. If the first 7 characters match, it returns 0, meaning they are equal.
  4. Since “Programming” and “Program” have the same first 7 characters, we print “The first 7 characters are equal.”

Output:

The first 7 characters are equal.

Conclusion

In this tutorial, we covered different ways to check if two strings are equal in C:

  1. Using strcmp() for full comparison.
  2. Manually comparing characters using a loop.
  3. Using strncmp() for partial comparison.