'The following code has some issue in implementation of overloaded minus operator

#include<iostream>
#include<cstring>
#include<utility>
class String {
    // think about the private data members...
    char* input;
    int length;
    
public:
    // provide definitions of following functions
    String() {
        input = NULL;
        length = 0;
    }
    // default constructor
    String(const char* str) {
        int size = strlen(str);
        char* string = new char[size];
        for (int i = 0; i < size; i++) {

            string[i] = str[i];
        }
        input = string;
        length = size;
    }
    // initializes the string with constant c-string
    String(const String& str) {
        input = str.input;
        length = str.length;
    }
    // copy constructor to initialize the string from the existing string

    String(int x) {
        length = x;
        input = new char[length];

    } // initializes a string of predefined size
    char& operator[](int i) {
        return *(input + i);
    }
    // returns the character at index [x]
    const char operator[](int i) const {
        return *(input + i);
    }
    // Assignment Operators
    void swap(String& other)
    {
        std::swap(input, other.input);
        std::swap(length, other.length);
    }

    String& operator=(char ch)
    {
        String newstr(1);
        newstr[0] = ch;
        newstr.swap(*this);
        return *this;
    }

    String& operator=(const char* str)
    {
        String(str).swap(*this);
        return *this;
    }

    String& operator=(const String& str)
    {
        if (this != &str)
            String(str).swap(*this);
        return *this;
    }

    String& operator=(String&& str)
    {
        String(std::move(str)).swap(*this);
        return *this;
    }
String& operator-(const String& substr) //remove the substr from the string
    {
        char* fd;
        int p = substr.length;
        fd = strstr(input, substr.input);
        if (fd)
        {
            int newsize = length - p;
            String newstr(newsize);
            int n = fd - input;
            for (int i = 0; i < n; i++)
                newstr.input[i] = input[i];
            int m = length - p - n;
            for (int i = n; i <= n + m; i++)
                newstr.input[i] = input[i + p];
        }
        return *this;
    }
    String& operator-(const string& substr) // remove the substr from the string
    {
        char* fd;
        int p = substr.size();
        fd = strstr(input, substr.c_str());
        if (fd)
        {
            int newsize = length - p;
            String newstr(newsize);
            int n = fd - input;
            for (int i = 0; i < n; i++)
                newstr.input[i] = input[i];
            int m = length - p - n;
            for (int i = n; i <= n + m; i++)
                newstr.input[i] = input[i + p];
            delete[]input;
            length = newsize;
            input = newstr.input;
        }
        return *this;
    }
int main()
{
String s1("ababacc");

    String s2("aba");
    String string2("cc");;
    s2 = s1 - s2;
    cout<< s2[0];
    cout<< s2[1];
    cout<<s2[2];
    s2 = s2 - string2;
    cout<< s2[0];
    cout<< s2[1];
}

I have overloaded the minus operator two times i.e String& operator-(const String& substr) and String& operator-(const string& substr). The cout statements in main functions are not giving the actual output. cout<<s2[0] is supposed to give 'b' in output. cout<< s2[1]; is supposed to give 'a' in output and cout<<s2[2] is supposed to give 'c' in output. But when I run this code I am not getting these actual values. What am I doing wrong?

c++


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source