C Program to Compare Two Strings

In this program, you'll learn to compare two strings. This strcmp() function is used to compare the string arguments. It compares strings lexicographically which means it compares both the strings character by character. It starts comparing the very first character of strings until the characters of both strings are equal or NULL character is found.



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

int main()
{
    int i;
    char str1[30], str2[30];

    printf("Enter the first string\n");
    scanf("%s", &str1);
    printf("Enter the second string\n");
    scanf("%s", &str2);

    if(strcmp(str1, str2) == 0)
    {
        printf("Both strings are EQUAL");
    }
    else
    {
        printf("Both strings are NOT EQUAL");
    }
    return 0;
}
Output
Enter the first string
rahul
Enter the second string
rahul
Both strings are EQUAL


Enter the first string
yellow
Enter the second string
yello
Both strings are NOT EQUAL





"Coding Hub - Learn to code" app now available on Google Play Store