#include "string.h"
#include <iostream>
using namespace std;
class String
{
public:
String(const char *str=NULL);
String(const String &other);
~String(void);
String& operator=(const String &other);
String& append (const String& str);
String& append (const String& str, size_t subpos, size_t sublen);
String& replace (size_t pos, size_t len, const String& str);
String &operator+(const String &str);
void ShowString();
private:
char *m_data;
};
//"+"运算符重载,实现append的功能
String &String::operator+(const String &str)
{
//
int len=strlen(str.m_data)+strlen(m_data)+1;
char *pTmpString=new char [len];
memcpy(pTmpString,m_data,strlen(m_data));
memcpy(pTmpString+strlen(m_data),str.m_data,strlen(str.m_data)+1);
delete m_data;
m_data=NULL;
m_data=pTmpString;
return *this;
}
String& String::append (const String& str)
{
//
int len=strlen(str.m_data)+strlen(m_data)+1;
char *pTmpString=new char [len];
memcpy(pTmpString,m_data,strlen(m_data));
memcpy(pTmpString+strlen(m_data),str.m_data,strlen(str.m_data)+1);
delete m_data;
m_data=NULL;
m_data=pTmpString;
return *this;
}
String& String::append (const String& str, size_t subpos, size_t sublen)
{
//
unsigned int substringLen=strlen(str.m_data);
if (subpos>substringLen||subpos<0)
{
return *this;
}
int sub_len=0;
if (subpos+sublen>substringLen)
{
//
sub_len=substringLen-subpos;
}else
{
sub_len=sublen;
}
int len=sub_len+strlen(m_data)+1;
char *pTmpString=new char [len];
memcpy(pTmpString,m_data,strlen(m_data));
memcpy(pTmpString+strlen(m_data),str.m_data+subpos,sub_len);
pTmpString[len-1]='\0';
delete m_data;
m_data=NULL;
m_data=pTmpString;
return *this;
}
String& String::replace (size_t pos, size_t len, const String& str)
{
//
unsigned int srcStringLen=strlen(m_data);
if (pos>srcStringLen)
{
//
cout<<"pos out of range"<<endl;
return *this;
}
//
int sub_len=0;
if (pos+len>srcStringLen)
{
//
sub_len=srcStringLen-pos;
}else
{
sub_len=len;
}
//
int length=strlen(m_data)-sub_len+strlen(str.m_data)+1;
char *pTmpString=new char [length];
memcpy(pTmpString,m_data,pos);
memcpy(pTmpString+pos,str.m_data,strlen(str.m_data));
memcpy(pTmpString+pos+strlen(str.m_data),m_data+pos+sub_len,srcStringLen-pos-sub_len);
pTmpString[length-1]='\0';
delete m_data;
m_data=NULL;
m_data=pTmpString;
//
return *this;
}
//
String::String(const char *str)
{
if (str==NULL)
{
m_data=new char[1];
m_data[0]='\0';
}else
{
int len=strlen(str);
m_data=new char[len+1];
strcpy(m_data,str);
}
}
String::String(const String &other)
{
//
int len=strlen(other.m_data);
m_data=new char[len+1];
strcpy(m_data,other.m_data);
}
String & String::operator =(const String &other)
{
if (&other!=this)
{
if (m_data!=NULL)
{
delete []m_data;
m_data=NULL;
}
int len=strlen(other.m_data);
m_data=new char[len+1];
strcpy(m_data,other.m_data);
}
return *this;
}
//
String::~String(void)
{
if (m_data!=NULL)
{
delete []m_data;
m_data=NULL;
}
}
void String::ShowString()
{
cout<<m_data<<endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
String mystring("hello world");
mystring.ShowString();
mystring=mystring+"hello";
String otherSring;
otherSring=mystring;
otherSring.ShowString();
String theOther(mystring);
theOther.ShowString();
mystring.append(mystring,3,20);
mystring.ShowString();
mystring.replace(9,50,otherSring);
return 0;
}
//String *myString4=(String *)malloc(sizeof(String)); //不会调用构造函数