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

From the Header file complete the Functions in your .cpp file. Do NOT use <cstri

ID: 3926862 • Letter: F

Question

From the Header file complete the Functions in your .cpp file. Do NOT use <cstring> library. recreate all functions and do NOT change any class specification. use buffer as your Destination array.

Follow the instructions below.

intialize - allocates the memory and sets the word parameter into the ADT. Also sets the wordLength.

deallocate - deallocates the memory used in the ADT.

copy - resizes (only if necessary) the destination buffer and copies the parameter's buffer into the calling ADT. Updates the wordLength as needed.

length - gets the wordLength.

concat - appends the String parameter's buffer to the end of the calling object's buffer. The destination buffer is resized (if needed) and wordLength is updated.

compare - compares the parameter's buffer to the calling object's buffer. Returns -1 if the calling ADT is less, 0 if both are the same, 1 if the calling ADT is greater.

print - prints out the ADT String.

9 #ifndef string-hpp 10 #define string..hpp 12 #include «stdio.h> 13 14 class Stringt 15 16 public: 17 18 19 20 int length) 21 void initialize(char *); void deallocate(); void copy(const String&); void concat (const String&); int compare(const String&); void print); 23 24 25 private: 26 27 28 ; 29 30 31 #endif /* string.hpp */ 32 char * buffer; int wordLength;

Explanation / Answer

#include <cstdlib>
#include <iostream>
#include <string>
#include "mystring.h"
#define string mystring

using namespace std;


void check (string s, string name){
cout << "checking " << name << endl;
cout << name << " contains " << s << endl;
cout << name << " capacity() is " << s.capacity() << endl;
cout << name << " length() is " << s.length() << endl;
cout << name << " size() is " << s.size() << endl;
cout << name << " max_size() is " << s.max_size() << endl << endl;

}

int main(int argc, char** argv) {

cout<<"This is Lab 5"<<endl;

string s1("Hello, World!");//step 2
string s1name("s1");//step 3

check(s1,s1name);//step 4
check(s1,s1name);//step 5

cout << "---Testing assignment operator--- ";
{
string s2;
s2=s1;
string s2name("s2");
check(s2,s2name);
if(s1==s2)
cout<<"comparison true ";
else
cout<<"comparison false ";
}

check(s1,s1name);

string s3("check assignment");
s3=s3;//checking to see if operator= is used when they are the same object.

check(s3,"s3");

cout<<"Lab 5 ends"<<endl;//step6

// //clear check
// s3.clear();
// check(s3,"s3");


// if(s1==s3)
// cout<<"comparison true ";
// else
// cout<<"comparison false ";

// reserve check
// mystring::size_type res;
// res=40;

s3.reserve(40);//still working on reserve
check(s3,"s3");

cout<<"in main buf size"<<s3.capacity()<<endl;

s3.reserve(5);
check(s3,"s3");

// char* test=s3.begin();
// cout<<&test<<endl;
// cout<<&s3<<endl;


//empty check
// string s4;
//
// if (s4.empty())
// cout<<"Empty is true ";
// else
// cout<<"Empty is false ";

return 0;
}

#ifndef MYSTRING_H
#define MYSTRING_H

#include <iostream>
#include <math.h>
#include <cstring>

using namespace std;

class mystring {
public:
// types with scope in this class
typedef unsigned int size_type;
typedef char * iterator;
typedef const char * const_iterator;
static const long int npos = 1073741824;

// default constructor
mystring();//good
// other constructors
mystring(const char *);//good
// copy constructor
mystring(const mystring& orig);//

// destructor
~mystring();////

// iterators

iterator begin();//good
iterator end();//good

//=== memory related ===

// change buffer size to n
void reserve(size_type n);

size_type size() const;////good returns len
size_type length() const;////good returns len
size_type capacity() const;////good returns buf_size
size_type max_size() const;////good
bool empty() const;////good

//=== overloading operators ===

// assignment operator
mystring& operator=(const mystring&);////
// mystring& operator=(const char *);

// array notation

char operator[](size_type pos) const;
char& operator[](size_type pos);

// append

mystring& operator+=(const mystring& str);
mystring& operator+=(const char * str);

//=== methods that modifiy the string

void clear();////good

void push_back(char c);


mystring& append(const mystring& str);
mystring& append(const char * str);

mystring& insert(size_type pos, const mystring& str);
mystring& insert(size_type pos, const char * str);

mystring& replace(size_type start, size_type span, const mystring& str);
mystring& replace(size_type start, size_type span, const char * str);

//=== conversion to c string

const char * c_str() const;//


private:
// pointer to the memory location where string is stored as a c-style
// string
char * ptr_buffer;
// the size of the memory in terms of bytes or characters capable of going into it currently
size_type buf_size;
// number of characters currently in the memory not including the
// terminating null character
size_type len;


};


#include "mystring.h"

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

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

}

// copy constructor to be implemented in lab 5
mystring::mystring(const mystring& orig) {

len=orig.length();
ptr_buffer=new char[len+1];
buf_size=len+1;

for(int n=0 ;n<buf_size; n++ )
{
ptr_buffer[n]=orig.ptr_buffer[n];
}
ptr_buffer[buf_size]='';
}

void mystring::reserve(size_type n)
{
cout<<"cccccc:"<<capacity()<<endl;
if( n > capacity() )
{
const char* temp = ptr_buffer;
ptr_buffer = new char[n];
memcpy(ptr_buffer, temp, len+1);
delete [] temp;
buf_size=n;
cout<<"bbbbbuf size"<<buf_size<<endl;
}

// char *temp;
//
// temp=new char[n];
//
// int i=0;
//
// for(;i<=len;i++)
// {
// temp[i]=ptr_buffer[i];
//
// }
// buf_size=n;
//
// delete [] ptr_buffer;
//
// ptr_buffer=temp;
//

}

mystring::iterator mystring::begin()//think is working correctly
{
iterator it=ptr_buffer;

return it;
}

mystring::iterator mystring::end()//think is working correctly
{
iterator it=ptr_buffer+len;

return it;
}

// one of the over loaded assignment operator to be implemented // assignment 3 (or for lab 5 if you have more time)
mystring& mystring::operator=(const mystring& orig){
if(this!=&orig)
// {
// cout<<"this==&mystring if statment activated ";//comment out after testing
// break;
// }
{
delete this->ptr_buffer;

this->len=orig.len;//length();
this->ptr_buffer=new char((this->len)+1);
this->buf_size=(this->buf_size)+1;
cout<<"Using assignment operator="<<endl;
for(int n=0;n<this->buf_size;n++)
{
this->ptr_buffer[n]=orig.ptr_buffer[n];
}
this->ptr_buffer[buf_size]='';
return *this;

}
}

// some simple methods provided for lab 5
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 to free space implemented for lab 5
mystring::~mystring() {
delete [] ptr_buffer;
}
// provided for lab 5 so we may cout mystring
ostream& operator<<(ostream& out, const mystring& str) {
out << str.c_str();
return out;
}
// provided for lab 5 to support the implementation of <<
const char * mystring::c_str() const {
return ptr_buffer;
}

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

}


void mystring::clear()
{
char *temp;
temp=new char[1];
temp[0]='';

ptr_buffer=temp;


buf_size=0;
len=0;
}


void mystring::push_back(char c)
{

}

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

}
return true;
}

return false;

}

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