Define a class String use overloaded operator to compare two strings

Improve Article

Save Article

  • Read
  • Discuss
  • Improve Article

    Save Article

    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++

    #include <cstring>

    #include <iostream>

    #include <string.h>

    using namespace std;

    class CompareString {

    public:

        char str[25];

        CompareString(char str1[])

        {

            strcpy(this->str, str1);

        }

        int operator==(CompareString s2)

        {

            if (strcmp(str, s2.str) == 0)

                return 1;

            else

                return 0;

        }

        int operator<=(CompareString s3)

        {

            if (strlen(str) <= strlen(s3.str))

                return 1;

            else

                return 0;

        }

        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;

        }

    }

    void testcase1()

    {

        char str1[] = "Geeks";

        char str2[] = "ForGeeks";

        CompareString s1(str1);

        CompareString s2(str2);

        cout << "Comparing \"" << s1.str << "\" and \""

             << s2.str << "\"" << endl;

        compare(s1, s2);

    }

    void testcase2()

    {

        char str1[] = "Geeks";

        char str2[] = "Geeks";

        CompareString s1(str1);

        CompareString s2(str2);

        cout << "\n\nComparing \"" << s1.str << "\" and \""

             << s2.str << "\"" << endl;

        compare(s1, s2);

    }

    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


    How can operators compare two strings?

    We simply use the equal to operator ( == ) to compare the two strings, which compares the value Bold to Bold and prints Equal.

    Can we use == operator to compare two strings?

    Using == operator: == operator used to check the reference equality of the two strings, whether they are pointing towards the same string object. Using compareTo() method: compareTo() method used to check the strings lexicographically, i.e. alphabetically.

    How to compare two strings in C++ using operator overloading?

    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.

    How do you overload an operator in a string class?

    Overloaded Operators in the string class String comparison ( == , != , > , < , >= , <= ): For example, you can use str1 == str2 to compare the contents of two string objects. Stream insertion and extraction ( << , >> ): For example, you can use cout << str1 and cin >> str2 to output/input string objects.