#include "mystring.h"
#include<iostream>
#include<cstring>
using namespace std;
MyString::MyString(const char*s)
{
str=new char[strlen(s)+1];
strcpy(str,s);
}
MyString::MyString()
{
str=nullptr;
}
MyString::MyString(const MyString &s)
{
str=new char[strlen(s.str)+1];
strcpy(str,s.str);
}
MyString::MyString(MyString &&s)
{
str=s.str;
s.str=nullptr;
}
MyString::~MyString()
{
delete[] str;
}
MyString &MyString::operator =(const MyString& s)
{
if(this==&s)
return *this;
delete[] str;
str=new char[strlen(s.str)+1];
strcpy(str,s.str);
return *this;
}
MyString &MyString::operator =(MyString &&s)
{
if(this==&s)
return *this;
delete[] str;
str=s.str;
s.str=nullptr;
return *this;
}
int MyString::size()const
{
return strlen(str);
}
MyString MyString::operator +(const MyString &s)const
{
int la=strlen(str);
int lb=strlen(s.str);
char *t=new char[la+lb+1];
strcpy(t,str);
strcat(t,s.str);
MyString p(t);
return p;
}
char &MyString::operator[](int index)
{
return str[index];
}
const char&MyString::operator[](int index)const
{
return str[index];
}
ostream&operator <<(ostream&out,const MyString& s)
{
out<<s.str;
return out;
}