Home > front end >  How to append a string at the end of string?
How to append a string at the end of string?

Time:04-23

#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;

class String
{
private:
    char* s;
    int size;
public:
    String()
    {
        s = NULL;
        size = 0;
    }

    String(const char* str)
    {
        size = strlen(str);
        s = new char[size];
        for (int i = 0; i < size; i  )
            s[i] = str[i];
    }

    String(const String& obj)
    {
        size = obj.size;
        s = new char[size];
        for (int i = 0; i < size; i  )
            s[i] = obj[i];
    }

    String(int x)
    {
        size = x;
        s = new char[size];
    }

    char& operator[](int i)
    {
        return *(s   i);
    }

    const char operator[](int i) const
    {
        return *(s   i);
    }

    String& operator (const char& str) // append a char at the end of string
    {
        int newsize = size   1;
        String newstr(size);
        for (int i = 0; i < size; i  )
        {
            newstr.s[i] = s[i];
        }
        newstr.s[size] = str;
        delete[]s;
        s = newstr.s;
        size = newsize;
        return *this;
    }

    String& operator (char*& str) // append a string at the end of string
    {
        int newsize = size   strlen(str);
        String newstr(newsize);
        for (int i = 0; i < size; i  )
        {
            newstr.s[i] = s[i];
        }
        for (unsigned i = 0; i < strlen(str); i  )
        {
            newstr.s[size   i] = str[i];
        }
        delete[]s;
        s = newstr.s;
        size = newsize;
        return *this;
    }

    operator int() const
    {
        int m;
        m = size;
        return m;
    }
};

int main()
{
    String s1("abcd");

    String s2;
    s2 = s1   'e';

    cout << s2[3];
    cout << s2[4];
    char* c = (char*)"asdfgh";
    s2 = s1   c;
    cout << s2[3];
    cout << s2[4];
    cout << s2[8];

}

My code runs perfectly until the statement char* c = (char*)"asdfgh". After this statement, I am not getting actual output. s2[3] after the statement char* c = (char*)"asdfgh" is supposed to give 'd' in the output, and similarly s2[4] is supposed to give 'a' in the output, and s2[8] is supposed to give 'g' in the output. But, the problem is, when I run this program, I am not getting these actual values.

Kindly look into this and tell me where I need to make changes.

CodePudding user response:

Your implementations of operator are both wrong. You are modifying the String object being added to, but you should instead be returning a new String that is the concatenation of the 2 input Strings. What you implemented would be more appropriate for an operator = instead. See What are the basic rules and idioms for operator overloading?

There are other problems with your code, too. Like a missing destructor and assignment operator, per the Rule of 3/5/0.

Try something more like this instead:

#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;

class String
{
private:
    char* s;
    size_t size;

public:
    String()
    {
        s = NULL;
        size = 0;
    }

    String(const char* str)
    {
        size = strlen(str);
        s = new char[size 1];
        for (int i = 0; i < size;   i)
            s[i] = str[i];
        s[size] = '\0';
    }

    String(const String& str)
    {
        size = str.size;
        s = new char[size 1];
        for (int i = 0; i < size;   i)
            s[i] = str[i];
        s[size] = '\0';
    }

    String(size_t x)
    {
        size = x;
        s = new char[size 1];
        s[size] = '\0';
    }

    ~String()
    {
        delete[] s;
    }

    String& operator=(const String &str)
    {
        if (this != &str)
        {
            String newstr(str);
            std::swap(s, newstr.s);
            std::swap(size, newstr.size);
        }
        return *this;
    }

    char& operator[](size_t i)
    {
        return s[i];
    }

    const char operator[](size_t i) const
    {
        return s[i];
    }

    String operator (char ch)
    {
        /*
        String newstr(*this);
        newstr  = ch;
        return newstr;
        */
        String newstr(size 1);
        for (int i = 0; i < size;   i)
        {
            newstr[i] = s[i];
        }
        newstr[size] = ch;
        return newstr;
    }

    String operator (const char* str)
    {
        /*
        String newstr(*this);
        newstr  = str;
        return newstr;
        */
        size_t slen = strlen(str);
        if (slen == 0) return *this;
        String newstr(size   slen);
        for (size_t i = 0; i < size;   i)
        {
            newstr[i] = s[i];
        }
        for (size_t i = 0; i < slen;   i)
        {
            newstr[size   i] = str[i];
        }
        return newstr;
    }

    String& operator =(char ch)
    {
        String newstr(size   1);
        for (size_t i = 0; i < size;   i)
        {
            newstr[i] = s[i];
        }
        newstr[size] = ch;
        std::swap(s, newstr.s);
        std::swap(size, s.size);
        return *this;
    }

    String& operator =(const char* str)
    {
        size_t slen = strlen(str);
        if (slen == 0) return *this
        String newstr(size   slen);
        for (size_t i = 0; i < size;   i)
        {
            newstr[i] = s[i];
        }
        for (size_t i = 0; i < slen;   i)
        {
            newstr[size   i] = str[i];
        }
        std::swap(s, newstr.s);
        std::swap(size, newstr.size);
        return *this;
    }

    size_t getSize() const
    {
        return size;
    }
};

int main()
{
    String s1("abcd");

    String s2;
    s2 = s1   'e';

    cout << s2[3];
    cout << s2[4];
    s2 = s1   "asdfgh";
    cout << s2[3];
    cout << s2[4];
    cout << s2[8];
}

CodePudding user response:

The reason it fails is actually earlier

String s1("abcd");
String s2;
s2 = s1   'e';

becuase your operator changes its lhs and then returns a reference to itself s2 is now a copy of s1. But they both point to the same 's' char array, things go bang shortly after that.

This would be fixed if you had an operator= that does the same as your copy constructor, but you dont have that

And as Remy points out, your operator semantics are wrong anyway. You should return 'newStr'

  •  Tags:  
  • c
  • Related