Comparing String objects using Relational Operators in C++ Last Updated : 28 Jun, 2017 Comments Improve Suggest changes Like Article Like Report 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 // 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 If you like GeeksforGeeks (We know you do!) and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to [email protected]. Comment More infoAdvertise with us Next Article Comparing String objects using Relational Operators in C++ S Sakshi Tiwari Improve Article Tags : C++ cpp-string Practice Tags : CPP Similar Reads Difference between Relational operator(==) and std::string::compare() in C++ Relational operators vs std::string::compare() Return Value: Relational operators return boolean value, while compare() returns unsigned integer.Parameters : Relational operators need only two strings to perform comparison, one which is being compared and other one is for reference, while compare() 2 min read C++ program to compare two Strings using Operator Overloading Pre-requisite: Operator Overloading in C++Given two strings, how to check if the two strings are equal or not, using Operator Overloading. Examples: Input: ABCD, XYZ Output: ABCD is not equal to XYZ ABCD is greater than XYZ Input: Geeks, Geeks Output: Geeks is equal to Geeks Approach: Using binary o 3 min read Results of comparison operations in C and C++ In C, data type of result of comparison operations is int. For example, see the following program. C #include<stdio.h> int main() { int x = 10, y = 10; printf("%d \n", sizeof(x == y)); printf("%d \n", sizeof(x < y)); return 0; } Output4 4 Whereas in C++, type of results 1 min read Ratio Manipulations in C++ | Set 2 (Comparison) Prerequisite - Ratio Manipulations in C++ | Set 1(Arithmetic)In C++, the <ratio> header file allows us to manipulate ratios using various inbuilt template alias. The header file was introduced from C++11 onwards. In this article, we will be discussing the Comparison of Ratio Manipulations in C 4 min read C++ Relational Operators In C++, Relational operators are used to compare two values or expressions, and based on this comparison, it returns a boolean value (either true or false) as the result. Generally false is represented as 0 and true is represented as any non-zero value (mostly 1).Syntax of Relational OperatorsAll C+ 3 min read Written version of Logical operators in C++ Can we use keywords in place of operators in C++ ? Yes, certainly, we can. The ANSI C++ Standard has proposed keywords for several C++ operators . They originated in C in the header at the time when there were keyboards that couldn't type the required symbols like &&, !, || etc. In C++, they became 2 min read std::string::compare() in C++ The string::compare() function in C++ is used to compare a string or the part of string with another string or substring. It is the member function of std::string class defined inside <string> header file. In this article, we will learn how to use string::compare() in C++.The different ways to 4 min read C++ Logical (&&, ||, !) Operator Overloading Prerequisites: OperatorsOperator Overloading Logical operators are used for combining two or more conditions or constraints or to complement the evaluation of the original condition in consideration. The result returns a Boolean value, i.e., true or false. In C++, there are 3 logical operators: Logi 3 min read How to Overload == Operator in C++? A class in C++ is the building block that leads to Object-Oriented programming. Class is a user-defined data type, which holds its own data members and member functions, which can be accessed and used by creating an instance of that class. The overloading of operators is a polymorphism that occurs a 2 min read unordered_set operators in C++ STL Unordered_set provides two operators in C++ STL. These are:Â Syntax:Â Â 1. (unordered_set &lhs == unordered_set &rhs) 2. (unordered_set &lhs != unordered_set &rhs) These operators are discussed in detail below:Â unordered_set == operator in C++ STL The â==â is an operator in C++ STL pe 5 min read Like