If strings are compared using relational operators then, their characters are compared lexicographically according to the current character traits, means it starts comparison character by character starting from the first character until the characters in both strings are equal or a NULL character is encountered.
- Parameters : Two Strings required to be compared. At left, one which is being compared and at right, another string with respect to which comparison is to be performed.
- Return type : Relational operator return either true or false value i.e. they return boolean values, true if the corresponding comparison holds, false otherwise.
List of Relational Operators:
- > : Greater than
- < : Less than
- == : Equal to
- != : Not equal to
- >= : Greater than and equal to
- <= : Less than and equal to
Important Conditions:
- s1 < s2 : A string s1 is smaller than s2 string, if either, length of s1 is shorter than s2 or first mismatched character is smaller.
- s1 > s2 : A string s1 is greater than s2 string, if either, length of s1 is longer than s2 or first mismatched character is larger.
- <= and >= have almost same implementation with additional feature of being equal as well.
- If after comparing lexicographically, both strings are found same, then they are said to be equal.
- If any of the points from 1 to 3 follows up then, strings are said to be unequal.
// CPP code to implement relational // operators on String objects #include<iostream> using namespace std; void relational_operation(string s1, string s2) { string s3 = s1 + s2; if(s1 != s2) cout << s1 << " is not equal to " << s2 << endl; if(s1 > s2) cout << s1 << " is greater than " << s2 << endl; else if(s1 < s2) cout << s1 << " is smaller than " << s2 << endl; if(s3 == s1 + s2) cout << s3 << " is equal to " << s1 + s2 << endl; } // Main function int main() { string s1("Geeks"); string s2("forGeeks"); relational_operation(s1, s2); return 0; } |
Output:
Geeks is not equal to forGeeks Geeks is smaller than forGeeks GeeksforGeeks is equal to GeeksforGeeks
This article is contributed by Sakshi Tiwari. If you like GeeksforGeeks (We know you do!) 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Rated as one of the most sought after skills in the industry, own the basics of coding with our C++ STL Course and master the very concepts by intense problem-solving.
Recommended Posts:
- Relational Operators on STL Array in C++
- Problem in comparing Floating point numbers and how to compare them correctly?
- Comparing two strings in C++
- Difference between Relational operator(==) and std::string::compare() in C++
- Set a variable without using Arithmetic, Relational or Conditional Operator
- Count the number of objects using Static member function
- How to sum two integers without using arithmetic operators in C/C++?
- Conditionally assign a value without using conditional and arithmetic operators
- How to restrict dynamic allocation of objects in C++?
- How to make a C++ class whose objects can only be dynamically allocated?
- When are static objects destroyed?
- C++ Classes and Objects
- Read/Write Class Objects from/to File in C++
- How to initialize Array of objects with parameterized constructors in C++
- Introduction to Complex Objects and Composition
- Life cycle of Objects in C++ with Example
- Passing and Returning Objects in C++
- What are the operators that can be and cannot be overloaded in C++?
- Increment (Decrement) operators require L-value Expression
- Order of operands for logical operators

