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.
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
char st1[20],st2[20];
cout<<"Enter first string : ";
cin>> st1;
cout<<"Enter second string: ";
cin>> st2;
if(strcmp(st1,st2)==0)
cout<<"\nBoth strings are equal";
else
cout<<"\nStrings are not equal";
return 0;
}
Enter first string : santosh
Enter second string: Santosh
Strings are not equal
Enter first string : Suraj
Enter second string: Suraj
Both strings are equal