Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need help cleaning my c++ code for mystring.cpp. I was told my boolean functio

ID: 3678996 • Letter: I

Question

I need help cleaning my c++ code for mystring.cpp. I was told my boolean function could be simplified to basically 1 line of strcmp function, but I'm not sure how to do that. Also I need help with my mystring::replace function simplification. Thank you!

=====================================================================

#include "mystring.h"

// default constructor
mystring::mystring()
{
ptr_buffer = new char[1];
*ptr_buffer = '';
buf_size = 1;
len = 0;
}

// constructor from c-style string
mystring::mystring(const char * s)
{
len = strlen(s);
buf_size = len + 1;
ptr_buffer = new char[buf_size];
strcpy(ptr_buffer, s);

}

// copy constructor
mystring::mystring(const mystring& orig)
{
len = orig.length();
ptr_buffer = new char[len+1];
buf_size = len+1;
strcpy(ptr_buffer, orig.ptr_buffer);
}

//Overload operators mystring objects can be manipulated as 1st class objects.
mystring& mystring::operator=(const mystring& orig)
{
delete [] ptr_buffer;
len = orig.length();
ptr_buffer = new char[len+1];
buf_size = len+1;
strcpy(ptr_buffer, orig.ptr_buffer);
}

mystring& mystring::operator=(const char* orig)
{
delete [] ptr_buffer;
len = strlen(orig);
buf_size = len + 1;
ptr_buffer = new char[buf_size];
strcpy(ptr_buffer, orig);
}

char mystring::operator[](size_type pos) const
{
return ptr_buffer[pos];
}

char& mystring::operator[](size_type pos)
{
return ptr_buffer[pos];
}

mystring& mystring::operator+=(const mystring& str)
{
this->append(str);
return *this;
}

mystring& mystring::operator+=(const char* str)
{
this->append(str);
return *this;
}

//Methods for modifying mystring objects with other mystring objects and c-style strings.
void mystring::clear()
{
len = 0;
ptr_buffer[0] = '';
}

void mystring::push_back(char c)
{
reserve(len + 1);
ptr_buffer[len] = c;
ptr_buffer[len + 1] = '';
len++;
}

mystring& mystring::append(const mystring& str)
{
return append(str.c_str());
}

mystring& mystring::append(const char* str)
{
int size = strlen(str);
reserve(size + len);
strcat(ptr_buffer, str);
len += size;
return *this;
}

mystring& mystring::insert(size_type pos, const mystring& str)
{
return insert(pos, str.c_str());
}

mystring& mystring::insert(size_type pos, const char* str)
{
int size = strlen(str);
reserve(len + size);
mystring b(ptr_buffer + pos);
  
for (int i = 0; i <= size; ++i)
{
ptr_buffer[i + len] = str[i];
}
  
len = (pos + size);
append(b);
return *this;
}

mystring& mystring::replace(size_type start, size_type span, const mystring& str)
{
char* temp = new char[buf_size];
  
for(int c = 0; c < buf_size; c++)
{
if(c >= start && (c-start) < span && str[c-start] != '')
temp[c] = str[c-start];
if(c < start || c >= (start + span))
temp[c] = ptr_buffer[c];
}
temp[buf_size-1] = '';
  
delete [] ptr_buffer;
ptr_buffer = temp;
temp = NULL;
}

mystring& mystring::replace(size_type start, size_type span, const char* str)
{
char* temp = new char[buf_size];
  
for(int c = 0; c < buf_size; c++) {
if(c >= start && (c-start) < span && str[c-start] != '')
temp[c] = str[c-start];
if(c < start || c >= (start + span))
temp[c] = ptr_buffer[c];
}
temp[buf_size-1] = '';
  
delete [] ptr_buffer;
ptr_buffer = temp;
temp = NULL;
}

void mystring::reserve(size_type n)
{
if(n < (len + 1))
return;
  
buf_size = n;
len = buf_size - 1;
char* temp = new char[buf_size];
strcpy(temp, ptr_buffer);
delete [] ptr_buffer;
ptr_buffer = temp;
temp = NULL;
}

//Methods to view properties of mystring objects.
mystring::size_type mystring::size() const
{
return len;
}

mystring::size_type mystring::length() const
{
return len;
}

mystring::size_type mystring::capacity() const
{
return buf_size;
}

mystring::size_type mystring::max_size() const
{
return (int)pow(2,30) -4 ;
}

bool mystring::empty() const
{
if(len == 0)
return true;
  
else
return false;
}

//Destructor
mystring::~mystring()
{
delete [] ptr_buffer;
}

//Boolean operators to compare mystring objectss and c-style strings.
// I need to clean this up with basically one strcmp function
bool operator==(const mystring& lhs, const mystring& rhs)
{
if(lhs.length() != rhs.length())
return false;
  
int i = 0;
while(lhs[i] != '') {
if(lhs[i] != rhs[i])
return false;
i++;
}
return true;
}

bool operator==(const char* lhs, const mystring& rhs)
{
if(strlen(lhs) != rhs.length())
return false;
  
int r = 0;
while(lhs[r] != '')
{
if(lhs[r] != rhs[r])
return false;
r++;
}
  
return true;
}

bool operator==(const mystring& lhs, const char* rhs)
{
if(lhs.length() != strlen(rhs))
return false;
  
int r = 0;
while(lhs[r] != '') {
if(lhs[r] != rhs[r])
return false;
r++;
}
  
return true;
}

bool operator!=(const mystring& lhs, const mystring& rhs)
{
if(lhs.length() != rhs.length())
return true;
  
int i = 0;
while(lhs[i] != '')
{
if(lhs[i] != rhs[i])
return true;
i++;
}
  
return false;
}

bool operator!=(const char* lhs, const mystring& rhs)
{
if(strlen(lhs) != rhs.length())
return true;
  
int r = 0;
while(lhs[r] != '')
{
if(lhs[r] != rhs[r])
return true;
r++;
}
  
return false;
}

bool operator!=(const mystring& lhs, const char* rhs)
{
if(lhs.length() != strlen(rhs))
return true;
  
int r = 0;
while(lhs[r] != '')
{
if(lhs[r] != rhs[r])
return true;
r++;
}
  
return false;
}

mystring operator+(const mystring& lhs, const mystring& rhs)
{
mystring concat = mystring(lhs);
concat.append(rhs);
return concat;
}

//Overload output operator to simplify displaying mystring objects.
ostream& operator<<(ostream& out, const mystring& str)
{
out << str.c_str();
return out;
}

//Method that returns c-style string of mystring object.
const char * mystring::c_str() const
{
return ptr_buffer;
}

mystring::iterator mystring::begin()
{
return ptr_buffer;
}

Explanation / Answer

Using strcmp function: strcmp function is used for comparing the lengths of two strings and is used as follows:

Just this 1 line code would be sufficient.

Now

if Return value < 0 then it indicates str1 is less than str2.

if Return value > 0 then it indicates str2 is less than str1.

if Return value = 0 then it indicates str1 is equal to str2.

Rest of your code is correct and would remain same.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote