C program to check given two string are equal or not

In this tutorial we are going to learn writing C program to check given two string are equal or not. There is multiple way to check this one.

In this tutorial we will learn two ways to do this. First we will see using the library function and second we will use own logic to compare.

How our string compare program will work?

  • Our program will take two string as an input which we have to compare.
  • We can directly compare with strcmp() function. It will return true if string match otherwise return false.
  • Using while loop and if else we have built own logic to check the string equality.
  • Here we are also considering uppercase and lowercase of the character.
  • If two character of string are same but have different case then program will return it as not equal.

C program to check given two string are equal or not without using strcmp()

#include <stdio.h>

int main()
{
    char str1[256], str2[256], c;
    int temp = 0, i = 0;

    printf("=== C Program to Check If Two Strings Are the Same ===\n");

    // Taking input for the first string
    printf("Enter the first string: ");
    scanf("%[^\n]", str1);  // Read input with spaces

    // Clear the input buffer before taking the second string
    getchar();  // Consume the newline character left by the previous input

    // Taking input for the second string
    printf("Enter the second string: ");
    scanf("%[^\n]", str2);  // Read input with spaces

    // Compare the strings character by character
    while (str1[i] != '\0' || str2[i] != '\0')  // Continue until one string ends
    {
        if (str1[i] != str2[i])  // If characters are not equal
        {
            temp = 1;  // Strings are different
            break;  // Exit the loop
        }
        i++;  // Move to the next character
    }

    // Output the result based on the comparison
    if (temp == 0)
        printf("Strings are the same.\n");
    else
        printf("Strings are not the same.\n");

    return 0;
}

Output

=== C Program to Check If Two Strings Are the Same ===
Enter the first string: quescol
Enter the second string: quescol
Strings are the same.

C program to check given two string are same or not using strcmp()

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

int main()
{
    char str1[256], str2[256];
    int temp = 0;

    printf("=== C program to check if the given two strings are the same or not ===\n");

    // Taking input for the first string
    printf("Enter the first string: ");
    scanf("%[^\n]", str1);  // Read input with spaces
    getchar();  // Consume the newline character left in the buffer

    // Taking input for the second string
    printf("Enter the second string: ");
    scanf("%[^\n]", str2);  // Read input with spaces

    // Compare the strings using strcmp
    if (strcmp(str1, str2) == 0)
        printf("Strings are the same\n");
    else
        printf("Strings are not the same\n");

    return 0;
}

Output

=== C program to check if the given two strings are the same or not ===
Enter the first string: quescol
Enter the second string: quescol
Strings are the same
What did you think?

Similar Reads

Hi, Welcome back!
Forgot Password?
Don't have an account?  Register Now