Comparing two strings in C++

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  : Beginner, forBeginner
Output : Beginner is not equal to forBeginner
         forBeginner is greater than Beginner

This problem can be solved using any of the following two methods  

  • 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 code
int main()
{
    string s1("Beginner");
    string s2("forBeginner");
    relationalOperation(s1, s2);
    string s3("Beginner");
    string s4("Beginner");
    relationalOperation(s3, s4);
    return 0;
}


Output

Beginner is not equal to forBeginner
forBeginner is greater than Beginner
Beginner is equal to Beginner

Time Complexity: O(min(n,m)) where n and m are the length of the strings.

Auxiliary Space:  O(max(n,m)) where n and m are the length of the strings.

This is because when string is passed in the function it creates a copy of itself in stack.

  • 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 Code
int main()
{
    string s1("Beginner");
    string s2("forBeginner");
    compareFunction(s1, s2);
    string s3("Beginner");
    string s4("Beginner");
    compareFunction(s3, s4);
    return 0;
}


Output

Beginner is not equal to forBeginner
forBeginner is greater than Beginner
Beginner is equal to Beginner

Time Complexity: O(min(n,m)) where n and m are the length of the strings.

Auxiliary Space:  O(max(n,m)) where n and m are the length of the strings.

This is because when string is passed in the function it creates a copy of itself in stack.

Differences between C++ Relational operators and compare() :-  

  1. compare() returns an int, while relational operators return boolean value i.e. either true or false.
  2. 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.
  3. 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";

The 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 the same manner.



Contact Us