joining 2 strings
Posted: Fri May 06, 2005 3:27 pm
i need to join two strings using the '+' operator.
i written the following which seems to be infested with bugs.
plz help.
i written the following which seems to be infested with bugs.
Code: Select all
Modified:--
class string
{
private:
char* str;
public:
string()
{
str=new char[1];
str[0]='\0';
}
string(const char* s)
{
int len=strlen(s);
str=new char[len+1];
strcpy(str,s);
}
int length()
{
return(strlen(str));
}
void input()
{
char t[100];
cin>>t;
int len=strlen(t)+1;
str=new char[len];
strcpy(str,t);
}
const char* text()
{
return(str);
}
string operator + (string s2)
{
string t;
int tlen=this->length()+s2.length()+1;
t.str=new char[tlen];
strcpy(t.str,this->str);
strcat(t.str,s2.str);
return(t);
}
};