• Register
1,240 points
10 7 4

Having two strings declared, they can be different or the same. If the characters contained by the strings are equal then they are the same otherwise they are not. And this is the approach we will be using to compare two strings.

Methods

We can compare strings using two different methods. The comparing methods can be by using a relational operator provided by C++ or by using the function compare. The approach or the logic is the same in both of the methods.

 

  1. Relational operators

The specific tasks are represented by operators. These are symbols with some specific meaning. Relational operators is the category of operators in C++, they determine the relation among different operands. A single relational operator can perform certain operations. Relational operators return boolean value. Boolean value means either the result is true or false. If we have to compare substrings many times in a program then using the relational operator is not convenient because we have to repeat long procedures of words in our program every time. 

#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; 
} 
  
// Main function 
int main() 
{ 
    string s1("firststring"); 
    string s2("secondstring"); 
    relationalOperation(s1, s2); 
    return 0; 
} 

Output: 

firststring is not equal to secondstring
secondstring  is greater than firststring

 

  1. std::compare()

The standard library provides different functions for our convenience. As the name of the std::compare() suggests that it is used to compare anything. It can be used to compare strings in a program. 

Using function compare has its own benefits like it can return integer values, can perform lots of different operations and we can compare any substring at any position in a given string using compare().

#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; 
} 
  
// Main function 
int main() 
{ 
    string s1("firststring"); 
    string s2("secondstring"); 
    compareFunction(s1, s2); 
    return 0; 
} 

Output:

firststring is not equal to secondstring
secondstring  is greater than firststring

Illustration of comparing substrings by example

We have to write less code if use compare()

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";