C Program to Compare Two Strings Using strcmp() Function

In this program, you'll learn how to compare two strings for equality using strcmp() function of string. The strcmp() is the C library function compares the string pointed to, by str1 to the string pointed to by str2. The function definition of strcmp() is: int strcmp(const char *str1, const char *str2) For example, s1 = "Hello," s2 = "How are you?" Then after string compare (means s1 == s2) the compiler will return false because both string are different.



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

int main()
{
    char st1[20],st2[20];
    printf("Enter first string : ");
    scanf("%s", st1);
    printf("Enter second string: ");
    scanf("%s", st2);
    if(strcmp(st1,st2)==0)
        printf("\nBoth strings are equal");
    else
        printf("\nStrings are not equal");
    return 0;
}
Output
Enter first string : santosh
Enter second string: Santosh

Strings are not equal

Enter first string : Suraj
Enter second string: Suraj

Both strings are equal





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