
#include <iostream>
#include <cstring>
using namespace std;
class MyString{
char *str=nullptr;
int size=0;
int capacity=0;
void reallocSize(int new_len){
if(new_len>capacity)
capacity=(capacity+1)*2;
if(str!=nullptr)
delete[] str;
size=new_len-1;
str=new char[capacity];
}
public:
MyString(){
reallocSize(1);
str[0]='\0';
}
MyString(const MyString &other){
reallocSize(other.size+1);
memcpy(str,other.str,other.size+1);
}
MyString(int size){
reallocSize(size);
}
MyString(char* s){
reallocSize(sizeof(s)+1);
memcpy(str,s,strlen(s)+1);
}
const MyString operator=(const MyString &other)const{
MyString temp(other);
memcpy(this->str,other.str,other.size+1);
return temp;
}
int length(){return size;}
const MyString operator+(const MyString &other)const{
MyString temp(this->size+other.size+1);
memcpy(temp.str,this->str,this->size);
memcpy(temp.str+this->size,other.str,other.size+1);
return temp;
}
int compare(const MyString &other)const{
for(int i=0;i<this->size&&i<other.size;i++)
if(this->str[i]!=other.str[i]) return this->str[i] > other.str[i] ? 1 : -1;
return 0;
}
~MyString(){
delete[] str;
str=nullptr;
}
friend std::ostream& operator<<(std::ostream& os,const MyString& other){
for(int i=0;i<other.size;i++){
os<<other.str[i];
}
return os;
}
friend std::istream& operator>>(std::istream& is,MyString& other){
is>>other.str;
other.size=strlen(other.str);
return is;
}
void show(){
cout<<this->str<<endl;
}
};
int main()
{
MyString s1;
MyString s2("12345678");
MyString s3("123456");
MyString s4("123");
s1.show(),s2.show();
cout<<s2.compare(s3)<<endl;
cout<<s2.length()<<endl;
cout<<s2<<endl;
cout<<s3+s4<<endl;
s2=s4;
cout<<s2<<endl;
MyString s5(20);
cin>>s5;
cout<<s5<<endl;
return 0;
}

