I have an assignment about the String Class overload function and operator.

Here is the problem i face:
Need an append() function to construct the String.
So the String constructor is:

String::String(char *s)
{
 init();
 append(s);
}
String& String::append(char *other)
{
 String temp;
 temp.m_length=strlen(other)+strlen(temp.m_buffer)+1;
 temp.m_buffer=new char[] m_length;
 if(!m_length)
 {
	 strcpy(temp.m_buffer,other);
	 return temp;
 }
 else
	 return temp;

}

So the compiler passing it but no correct result?
So here's append() is similar to operator +=??

Here is the assignment:
Consider what the return value should be. You should be able to write code like:
String s1 = "foo";
String s2 = "bar";
s1.append (s2);
now s1 is "foobar"
String s3 = "moo";
s3.append ("quack").append ("meow");
now s3 is "mooquackmeow"

Anybody knows how to do that append() function??
thanks so much

Recommended Answers

All 9 Replies

>>temp.m_buffer=new char[] m_length;

Shouldn't that be this: temp.m_buffer=new char[m_length]; >>if(!m_length)
That means if m_length == 0. When that happens strlen(other) == 0 and the previous new operator will return a pointer to a 0-length character array. So if the length of the allocated buffer is 0, how in the world can you expect the strcpy() to succeed?

posible suggested change: (Not compiled or tested, you will have to do that).

String& String::append(char *other)
{
  
 int newlen = strlen(other)+ m_length + 1;
 char *temp = new char[newlen];
 temp[0] = 0;
 if( m_length > 0)
 {
    // move existing buffer to temp, then delete m_buffer
     strcpy(temp, m_buffer);
     delete[] m_buffer;
     m_length = 0;
 }
 else
       temp[0] = 0;
  strcat(temp, other);
  m_buffer = temp;
  m_length = newlen;
  return *this;
}

I have no idea how to do that now, the assignment is the statement i told you guys before.
Here is my code:

class String  
{
  friend ostream& operator<< (ostream& os, const String& s);

public:
  String();
  String (char* s);
  String (const String& s);
  explicit String (char ch, size_t count /*= 1*/);
  ~String();
  size_t length() const;
  String& append(const String& src);
  String& append(const char* src);
private:
  void init();
  char* m_buffer;
  size_t m_length;
};

String::String()
{
  init();
}

String::String(char* s)
{
  init();
  append(s);
}

String::String(const String& s)
{
  init();
  append(s);
}

String& String::append (const char* src)
{
 	char* temp;
	m_length=strlen(m_buffer)+strlen(src)+1;
	temp=new char[m_length];
	strcpy(temp,m_buffer);
	strcat(temp,src);
	m_buffer=temp;
	return *this;
}

String& String::append(const String& src)
{
    char* temp;
	m_length=strlen(m_buffer)+strlen(src.m_buffer)+1;
	temp=new char[m_length];
	strcpy(temp,m_buffer);
	strcat(temp,src.m_buffer);
	m_buffer=temp;
}

void String::init()
{
  m_buffer = NULL;
  m_length = 0;
  m_bufSize = 0;
}

and pass the compiler but could have correct results. Here is my main()

int main()
{
   cout << endl << "TEST CASE 2:" << endl; 
   String ditty ("Are you sleeping?\n");
}

Re-read my previous post.

use the suggestion you give me.
its working fine when i did:

String string1;
String string2("hello");
string2.append(string1);

but it didn't work out if i did: string1.append(string1);

Here is my code details:

int main()
{
 String string3;
 string3 = "hello";
 String string4("world");
 string3.append(string3);//didn't pass
 string4.append(string4);//didn't pass
 string4.append(string3);//pass

}

Here is my main class:

class String 
{
public:
  String();
  String (char* s);
  String (const String& s);
  explicit String (char ch, size_t count /*= 1*/);
  ~String();
  size_t length() const;
  String& append(const String& other);
  String& append(const char* other);
 String& operator = (const char *other);
 String& operator = (const String& other);
 private:
  void init();
  char* m_buffer;
  size_t m_length;
}

Here is the constructor and the implementation

String::String()
{
  init();
}

String::String(char* s)
{
  init();
  append(s);
}

String::String(const String& s)
{
  init();
  append(s);
}

String::String(char ch, size_t count)
{
  init();
  memset(m_buffer,ch,count);
  m_length=count;
} 

String::~String()
{
  delete[] m_buffer;
}

String& String::append(const char *other)
{
 
 int newlen = strlen(other)+ m_length + 1;
 char *temp = new char[newlen];
 temp[0] = 0;
 if( m_length > 0)
 {
    // move existing buffer to temp,then delete m_buffer
     strcpy(temp, m_buffer);
     delete[] m_buffer;
     m_length = 0;
 }
 else
       temp[0] = 0;
  strcat(temp, other);
  m_buffer = temp;
  m_length = newlen;
  return *this;
}


String& String::append(const String& other)
{
 int newlen = strlen(other.m_buffer)+ m_length + 1;
 char *temp = new char[newlen];
 temp[0] = 0;
 if( m_length> 0)
 {
     strcpy(temp,m_buffer);
     delete[] m_buffer;
     m_length = 0;
 }
 else
       temp[0] = 0;
  strcat(temp,other.m_buffer);
  m_buffer = temp;
  m_length = newlen;
  return *this;
}

String &String::operator = (const String& other)
{ 
 
    if(this == &other)
	{ return *this;}
	else
	{
        if(m_buffer!=NULL)
       {
         delete [] m_buffer;
         m_length=strlen(other.m_buffer) + 1;
         m_buffer = new char[m_length];
         strcpy(m_buffer,other.m_buffer);
        }
        else
        {
          m_length=strlen(other.m_buffer) + 1;
          m_buffer = new char[m_length];
          strcpy(m_buffer,other.m_buffer);
        }
       
      return *this;
	}

}


String& String::operator = (const char *other)
{
	if(this->m_buffer == other)
	{return *this;}
	else
	{
        if(m_buffer!= NULL)
        {
         delete [] m_buffer;
         m_length=strlen(other)+1;
         m_buffer=new char[m_length];
         strcpy(m_buffer,other);
        }
        else
        {
            m_length=strlen(other)+1;
            m_buffer=new char[m_length];
            strcpy(m_buffer,other);
        }
          return *this;   
    }
 
}

Have no idea where is the problem is??
the assignment is like i said before

The problem is that m_buffer is being deleted too early. Here is the correction. I had not anticipated that you would make the parameter to the append function a reference to itself.

String& String::append(const String& other)
{
 int newlen = strlen(other.m_buffer)+ m_length + 1;
 char *temp = new char[newlen];
 temp[0] = 0;
 if( m_length> 0)
 {
     strcpy(temp,m_buffer);
//     delete[] m_buffer;
//     m_length = 0;
 }
 else
       temp[0] = 0;
 if( other.m_length > 0)
      strcat(temp,other.m_buffer);
 delete[] m_buffer;
 m_buffer = temp;
 m_length = newlen;
 return *this;
}

Here have another problem i face and i don't know why.
Basically this is my assignment and i couldn't change a lot of things.
here is my code:

//this is one kind of constructor--this one i couldn't change;
String::String(char ch, size_t count /* = 1 */)
{
  memset(m_buffer,ch,count);
  m_length=count;
}
//this is overload operator
String &String::operator = (const String& other) 
{  
  
	if(this->m_buffer!=other.m_buffer)
   {
		if(m_buffer!=NULL)
	   {
		 delete [] m_buffer; 
		 m_length=strlen(other.m_buffer) + 1;
		 m_buffer = new char[m_length];
		 strcpy(m_buffer,other.m_buffer);
		}
		else
		{
		  m_length=strlen(other.m_buffer) + 1;
		  m_buffer = new char[m_length];
		  strcpy(m_buffer,other.m_buffer);
		}
		
      return *this;
   }
   else
	  return *this;

}


String& String::operator = (const char *other)
{
	if(this->m_buffer!=other)
	{ 
		if(m_buffer!= NULL)
		{
		 delete [] m_buffer;
		 m_length=strlen(other)+1;
		 m_buffer=new char[m_length];
		 strcpy(m_buffer,other);
		}
		else
		{
			m_length=strlen(other)+1;
		    m_buffer=new char[m_length];
			strcpy(m_buffer,other);
		}
	
	   return *this;	
	}
	else
		return *this;


}

and the driver couldn't pass:

int main()
{
 String ditty;//this couldn't change
 ditty = String('p',0);//this one also couldn't change
}

thx so much!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.