Relational Operator on String Objects in C++

Several string comparison operators are defined in the string class and these operators operate by comparing the string characters and all these comparisons are case-sensitive. But one extra point to keep in mind is that when we do a comparison of two strings using a relational operator then comparison takes place lexicographically.


What is lexicographical order?

Lexicographical order is basically a dictionary order of arranging words. For example, In a dictionary, the word 'Accutane' will come before 'Accutron' because the letter 'a' comes before 'r' in the English alphabetic system.


List of Relational Operators:

  • Equal to (==): This operator is used to test the equality of two strings.
  • Not Equal to (!=): This operator is used to test whether two strings are unequal. 

Note: Two strings are said to be equal when they have the same length and contain the same characters.


C++ example code:

/*C++ program to check equality operator*/
#include<iostream>
#include<string>
using namespace std;

int main(){
  string str1 = "Welcome to Algolesson";
  string str2 = "welcome to algolesson";

  if(str1 == str2)
     cout<<"Both the string are equal"<<endl;

  if(str1 != str2)
    cout<<"Both the strings are not equal"<<endl;   
  
  return 0;
}
Output:
Both the strings are not equal

More Relational Operators:
  • Greater than(>): This operator is used to check whether one string is greater than another string.
  • Lesser than (<): This operator is used to check whether one string is lesser than another string.
  • Greater than and equal to (>=): This operator is used to check whether one string is greater than or equal to another string.
  • Lesser than and equal to (>=): This operator is used to check whether one string is lesser than or equal to another string.
Note: One string is smaller than the other string when the length of the first string is smaller than the second string or the first mismatched character of the first string is smaller than the character of the second string. 

Let's understand more C++ code implementation,
/*C++ program to check relational operator*/
#include<iostream>
#include<string>
using namespace std;

int main(){
  string str1 = "Accutane";
  string str2 = "Accutron";
  string str3 = "Axe";

  if((str1 > str2) && (str1 > str3))
    cout<<"Largest string is: "<<str1<<endl;
  else if((str2 > str1) && (str2 > str3))
    cout<<"Largest string is: "<<str2<<endl;
  else
    cout<<"Largest string is: "<<str3<<endl;

  return 0;
}
Output:
Largest string is: Axe

In the above code, if you observe the output is the third string that is "Axe" considered the longest string out of all three but the length of the third string is smaller than the other two strings but still, it is the largest because if you compare all three strings the second character of the third string that is 'x' is bigger than the second character of other two strings.
I hope you are now able to understand how string comparison takes place in lexicographical order when we use relational operators.

Next:

 (getButton) #text=(String Member Functions in C++) #icon=(link) #color=(#2339bd)

⚡ Please share your valuable feedback and suggestion in the comment section below or you can send us an email on our offical email id ✉ algolesson@gmail.com. You can also support our work by buying a cup of coffee ☕ for us.

Similar Posts

No comments:

Post a Comment


CLOSE ADS
CLOSE ADS