"Mystring.h": #Include #Include #Include Namespace Const Char
"Mystring.h": #Include #Include #Include Namespace Const Char
#include <cstring>
#include "mystring.h"
namespace main_savitch_4
{
mystring::mystring(const char str[])
// The constructor (default argument is an empty string.)
// Precondition: str is an ordinary null-terminated string.
// Postcondition: The string contains the sequence of chars from str.
{
size_t size = strlen(str);
sequence = new char[size + 1];
strcpy(sequence, str);
current_length = size;
allocated = current_length + 1;
current_length = source.current_length;
allocated = source.allocated;
mystring::~mystring()
// destructor
// Precondition: a mystring object has been dynamically allocated in memory
// Postcondition: the array created by the mystring object has been safely
deleted
{
delete[] sequence;
sequence = 0;
}
void mystring::operator += (const mystring& addend)
// Postcondition: addend has been catenated to the end of the string.
{
if (current_length + addend.current_length > allocated - 1)
reserve(current_length + addend.current_length);
// All overloaded operator += functions check to see if more space
// is needed before allocating any additional memory
strcat(sequence, addend.sequence);
current_length += addend.current_length;
}
void mystring::operator +=(const char addend[])
// Precondition: addend is an ordinary null-terminated string.
// Postcondition: addend has been catenated to the end of the string.
{
if (current_length + strlen(addend) > allocated - 1)
reserve(current_length + strlen(addend));
strcat(sequence, addend);
current_length += strlen(addend);
}
void mystring::operator +=(char addend)
// Postcondition: The single character addend has been catenated to the
// end of the string.
{
if (current_length + 1 > allocated - 1)
reserve(current_length + 1);
sequence[current_length] = addend;
sequence[current_length + 1] = '\0';
current_length++;
}
void mystring::reserve(size_t n)
// Postcondition: All functions will now work efficiently(without
// allocating new memory) until n characters are in the string.
{
if (n > current_length)
// A larger sequence is created only if the extra space is requested
{
char *new_seq;
new_seq = new char[n + 1];
strcpy(new_seq, sequence);
delete[] sequence;
sequence = new_seq;
allocated = n + 1;
}
}
void mystring::operator =(const mystring& source)
// Postcondition: A mystring object is returned with the same
// member data as source and a different pointer
{
if (current_length == source.current_length)
strcpy(sequence, source.sequence);
else
{
char *temp;
temp = new char[source.allocated];
strcpy(temp, source.sequence);
delete[] sequence;
sequence = temp;
current_length = source.current_length;
allocated = source.allocated;
}
}
char mystring::operator [ ](size_t position) const
// Precondition: position < length( ).
// Postcondition: The value returned is the character at the specified
// position of the string. A string's positions start from 0 at the start
// of the sequence and go up to length( )-1 at the right end.
{
return sequence[position];
}
return outs;
//
//
//
//
//
//
{
Postcondition: A string has been read from the istream ins, and the
istream ins is then returned by the function. The reading operation
skips white space (i.e., blanks, newlines, tabs) at the start of ins.
Then the string is read up to the next white space or the end of the
file. The white space character that terminates the string has not
been read.
char c;
while ((ins) && isspace(ins.peek()))
ins.ignore();
while (ins && ((ins.peek()) != '\n'))
{
ins >> c;
target += c;
}
return ins;
ins >> c;
target += c;