Compare two strings - C++ Program

Q. Write a C++ program to compare two strings.

Answer:

Following program is comparing two strings using strcmp() library function. This function is used to compare the two strings with each other.

#include<iostream>
#include<string.h>
#include<stdio.h>
using namespace std;
int main()
{
        char str1[100], str2[100];
        cout<<"\n Enter First String : ";
        gets(str1);
        cout<<"\n Enter Second String : ";
        gets(str2);
        if(strcmp(str1, str2)==0)  //strcmp() is a library function, used to compare the two strings.
        {
                cout<<"\n Strings are Equal!!!";
        }
        else
        {
                cout<<"\n Strings are Not Equal!!!";
        }
        return 0;
}


Output:

compare string 1

compare string 2