Comparing String objects using Relational Operators in C++ Last Updated : 07 Oct, 2025 Comments Improve Suggest changes 5 Likes 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. Relational operator return either true or false value, true if the corresponding comparison holds, false otherwise. C++ #include <iostream> using namespace std; void compreStrs(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; } int main() { string s1("geeks"); string s2("forgeeks"); compreStrs(s1, s2); return 0; } Outputgeeks is not equal to forgeeks geeks is greater than forgeeks geeksforgeeks is equal to geeksforgeeks 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 and equality is checked using == Create Quiz Comment K kartik 5 Improve K kartik 5 Improve Article Tags : C++ cpp-string Explore C++ BasicsIntroduction to C++3 min readData Types in C++6 min readVariables in C++4 min readOperators in C++9 min readBasic Input / Output in C++3 min readControl flow statements in Programming15+ min readLoops in C++7 min readFunctions in C++8 min readArrays in C++8 min readCore ConceptsPointers and References in C++5 min readnew and delete Operators in C++ For Dynamic Memory5 min readTemplates in C++8 min readStructures, Unions and Enumerations in C++3 min readException Handling in C++12 min readFile Handling in C++8 min readMultithreading in C++8 min readNamespace in C++5 min readOOP in C++Object Oriented Programming in C++8 min readInheritance in C++6 min readPolymorphism in C++5 min readEncapsulation in C++3 min readAbstraction in C++4 min readStandard Template Library(STL)Standard Template Library (STL) in C++3 min readContainers in C++ STL2 min readIterators in C++ STL10 min readC++ STL Algorithm Library3 min readPractice & ProblemsC++ Interview Questions and Answers1 min readC++ Programming Examples4 min read Like