C++ program to compare two Strings using Operator Overloading Last Updated : 18 May, 2021 Comments Improve Suggest changes Like Article Like Report 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 operator overloading. Declare a class with a string variable and operator function ‘==’, '<=' and '>=' that accepts an instance of the class and compares it’s variable with the string variable of the current instance.Create two instances of the class and initialize their class variables with the two input strings respectively.Now, use the overloaded operator(==, <= and >=) function to compare the class variable of the two instances. Below is the implementation of the above approach: C++ // C++ program to compare two Strings // using Operator Overloading #include <cstring> #include <iostream> #include <string.h> using namespace std; // Class to implement operator overloading // function for concatenating the strings class CompareString { public: // Classes object of string char str[25]; // Parameterized Constructor CompareString(char str1[]) { // Initialize the string to class object strcpy(this->str, str1); } // Overloading '==' under a function // which returns integer 1/true // if left operand string // and right operand string are equal. //(else return 0/false) int operator==(CompareString s2) { if (strcmp(str, s2.str) == 0) return 1; else return 0; } // Overloading '<=' under a function // which returns integer 1/true // if left operand string is smaller than // or equal to the right operand string. // (else return 0/false) int operator<=(CompareString s3) { if (strlen(str) <= strlen(s3.str)) return 1; else return 0; } // Overloading '>=' under a function // which returns integer 1/true // if left operand string is larger than // or equal to the right operand string. //(else return 0/false) int operator>=(CompareString s3) { if (strlen(str) >= strlen(s3.str)) return 1; else return 0; } }; void compare(CompareString s1, CompareString s2) { if (s1 == s2) cout << s1.str << " is equal to " << s2.str << endl; else { cout << s1.str << " is not equal to " << s2.str << endl; if (s1 >= s2) cout << s1.str << " is greater than " << s2.str << endl; else cout << s2.str << " is greater than " << s1.str << endl; } } // Testcase1 void testcase1() { // Declaring two strings char str1[] = "Geeks"; char str2[] = "ForGeeks"; // Declaring and initializing the class // with above two strings CompareString s1(str1); CompareString s2(str2); cout << "Comparing \"" << s1.str << "\" and \"" << s2.str << "\"" << endl; compare(s1, s2); } // Testcase2 void testcase2() { // Declaring two strings char str1[] = "Geeks"; char str2[] = "Geeks"; // Declaring and initializing the class // with above two strings CompareString s1(str1); CompareString s2(str2); cout << "\n\nComparing \"" << s1.str << "\" and \"" << s2.str << "\"" << endl; compare(s1, s2); } // Driver code int main() { testcase1(); testcase2(); return 0; } Output: Comparing "Geeks" and "ForGeeks" Geeks is not equal to ForGeeks ForGeeks is greater than Geeks Comparing "Geeks" and "Geeks" Geeks is equal to Geeks Comment More infoAdvertise with us Next Article C++ program to compare two Strings using Operator Overloading A Anirban166 Follow Improve Article Tags : Strings Technical Scripter C++ DSA cpp-operator-overloading Operator Overloading cpp-overloading +3 More Practice Tags : CPPStrings Similar Reads Comparing String objects using Relational Operators in C++ 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 i 2 min read Rules for operator overloading In C++, following are the general rules for the things that are not allowed with operator overloading. 1) Only built-in operators can be overloaded. New operators can not be created. 2) Arity of the operators cannot be changed. 3) Precedence and associativity of the operators cannot be changed. 4) O 3 min read Check if one string can be converted to other using given operation Given two strings S and T of same length. The task is to determine whether or not we can build a string A(initially empty) equal to string T by performing the below operations. Delete the first character of S and add it at the front of A. Delete the first character of S and add it at the back of A. 15+ 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 Compare two integers without using any Comparison operator Given two integers A & B. Task is to check if A and B are same or not without using comparison operators.Examples: Input : A = 5 , B = 6Output : 0 Input : A = 5 , B = 5 Output : 1 Note : 1 = "YES" and 0 = "NO" The idea is pretty simple we do Xor of both elements ( A, B ). if Xor is zero then two 5 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 Find uncommon characters of the two strings Given two strings s1 and s2, the task is to find the uncommon characters in both strings. An uncommon character means that the character is present in only one string or in another string but not in both. The strings contain only lowercase characters and can have duplicates. Note: Output the uncommo 12 min read Operator Overloading in C++ in C++, Operator overloading is a compile-time polymorphism. It is an idea of giving special meaning to an existing operator in C++ without changing its original meaning.In this article, we will further discuss about operator overloading in C++ with examples and see which operators we can or cannot 8 min read Check if two strings are same or not without using library functions Given two strings S1 and S2, the task is to check whether they are the same or not without using string library functions. Examples: Input: S1 = âGeeksForGeeksâ, S2 = âGeeksForGeeksâOutput: TrueExplanation:S1 and S2 are the same strings Input: S1 = âGeeksForGeeksâ, S2 = âGeeksforGeeksâOutput: False 5 min read Compare two strings considering only alphanumeric characters Given two strings that can contain lower and uppercase alphabets, numbers and special characters like dots, blank spaces, commas, etc. Compare both strings considering only alphanumeric characters([a-b], [A-B] and [0-9]) if they are equal or not. For example, strings "Ram, Shyam" and "Ram-Shyam" bot 10 min read Like