Given two strings, how to check if the two strings are equal or not.
Examples:
Input : ABCD, XYZ
Output : ABCD is not equal to XYZ
XYZ is greater than ABCD
Input : Geeks, forGeeks
Output : Geeks is not equal to forGeeks
forGeeks is greater than Geeks
This problem can be solved using any of following two methords
- C++ Relational operators
CPP
// CPP code to implement relational// operators on string objects#include <iostream>using namespace std;void relationalOperation(string s1, string s2){ if (s1 != s2) { cout << s1 << " is not equal to " << s2 << endl; if (s1 > s2) cout << s1 << " is greater than " << s2 << endl; else cout << s2 << " is greater than " << s1 << endl; } else cout << s1 << " is equal to " << s2 << endl;}// Driver codeint main(){ string s1("Geeks"); string s2("forGeeks"); relationalOperation(s1, s2); string s3("Geeks"); string s4("Geeks"); relationalOperation(s3, s4); return 0;} |
Geeks is not equal to forGeeks forGeeks is greater than Geeks Geeks is equal to Geeks
- std:: Compare()
CPP
// CPP code perform relational// operation using compare function#include <iostream>using namespace std;void compareFunction(string s1, string s2){ // comparing both using inbuilt function int x = s1.compare(s2); if (x != 0) { cout << s1 << " is not equal to " << s2 << endl; if (x > 0) cout << s1 << " is greater than " << s2 << endl; else cout << s2 << " is greater than " << s1 << endl; } else cout << s1 << " is equal to " << s2 << endl;}// Driver Codeint main(){ string s1("Geeks"); string s2("forGeeks"); compareFunction(s1, s2); string s3("Geeks"); string s4("Geeks"); compareFunction(s3, s4); return 0;} |
Geeks is not equal to forGeeks forGeeks is greater than Geeks Geeks is equal to Geeks
Differences between C++ Relational operators and compare() :-
- compare() returns int, while relational operators return boolean value i.e. either true or false.
- A single Relational operator is unique to a certain operation, while compare() can perform lots of different operations alone, based on the type of arguments passed.
- We can compare any substring at any position in a given string using compare(), which otherwise requires the long procedure of word by word extraction of string for comparison using relational operators.
Example :-
- Using compare()
// Compare 3 characters from 3rd position // (or index 2) of str1 with 3 characters // from 4th position of str2. if (str1.compare(2, 3, str2, 3, 3) == 0) cout<<"Equal"; else cout<<"Not equal";
- Using Relational operator
for (i = 2, j = 3; i <= 5 && j <= 6; i++, j++)
{
if (s1[i] != s2[j])
break;
}
if (i == 6 && j == 7)
cout << "Equal";
else
cout << "Not equal";
Above example clearly shows how compare() reduces lots of extra processing, therefore it is advisable to use it while performing substring comparison at some position, otherwise both perform almost in same manner.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Comparing String objects using Relational Operators in C++
- Pairs of complete strings in two sets of strings
- Meta Strings (Check if two strings can become same after a swap in one string)
- Number of common base strings for two strings
- Count of strings that become equal to one of the two strings after one removal
- Count of same length Strings that exists lexicographically in between two given Strings
- Check whether two strings can be made equal by reversing substring of equal length from both strings
- Search in an array of strings where non-empty strings are sorted
- Count of distinct Strings possible by swapping prefixes of pairs of Strings from the Array
- Distinct strings such that they contains given strings as sub-sequences
- Print all Strings from array A[] having all strings from array B[] as subsequence
- Search strings with the help of given pattern in an Array of strings
- Print all strings of maximum length from an array of strings
- Append digits to the end of dupicate strings to make all strings in an array unique
- Print all interleavings of given two strings
- Interleaving of two given strings with no common characters
- Check if edit distance between two strings is one
- Minimum Cost To Make Two Strings Identical
- Program to add two binary strings
- Find uncommon characters of the two strings
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : DeepjyotSinghKapoor

