Write a C++ program to compare two strings they are exact equal or not.

Program Code:

#include<iostream>
using namespace std;
int main( )
{
int i;
char name1[30], name2[30];
cout<<"Enter your first string: ";
cin.getline(name1, 30);
cout<<endl;
cout<<"Enter your second string: ";
cin.getline(name2, 30);
cout<<endl;
for (i = 0; name1[i] == name2[i] && name1[i]!= '\0' && name2[i] != '\0'; i++);
if(name1[i] - name2[i] == 0)
{
cout << "Strings are equal";
}
else
{
cout << "Strings are not equal";
}
return 0;
}

This program output is tested on www.jdoodle.com

Output:
Enter your first string: abc
Enter your second string: xyz
Strings are not equal


Thanks
Mukesh Rajput

Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments: