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

CS142 Assignment 10 Spring 2018 Given: Sat, Apr 21 Due: Thu, Apr 26, 2:30 p.m. 1

ID: 3716739 • Letter: C

Question

CS142 Assignment 10 Spring 2018 Given: Sat, Apr 21 Due: Thu, Apr 26, 2:30 p.m. 1. (100%) Create a class string similar to the C++ class string. Include the following operations. These operations are described in Section 3.2 of the note:s Tip: Consider how you will implement the method c_str (see part k below) before deciding how you will store the characters of your String objects (a) A default constructor (b) A constructor that takes a C string as argument. (c) A copy constructor (if needed) (d) A destructor (if needed). (e) The method length ). (f) An indexing operator. (g) The method clear (). (h) The operator +. (i) An assignment operator (if needed) (j) The version of method replace (i, m, s2) in which the third argument is a String object. (k) The method c_str. (This method should return a pointer to a C string that is stored in the String object itself.) The easiest way to implement this class is to use an STL vector to store the characters of the string. But, for the purposes of this assignment, you are not allowed to do that. Instead, you have to directly use a dynamically allocated array. Split your class into header and implementation file in the usual way. Sub- mit your test driver

Explanation / Answer

SO here is the program to implement String class functionality

.h file content

#ifndef mystring_h
#define mystring_h

class MyString
{
public:
  int size_str;
  char *str;
  
MyString()                                           // (a) a default constructor
{
  str=NULL;
  size_str=0;
  }
  
    MyString(const char *usrstr);                        // (b) a constructor that take c-MyString as argument


MyString(MyString &usrstr);                             // (c) a copy constructor that take class own object as argument



MyString& operator = (const MyString& usrstr) ;        // (i) Assignment operator overloaded to tackle situation like MyString //s2=s1;

MyString operator + (const MyString& usrstr1);

char *c_str(); // (k)function that return c styke string on calling

int length();                                              // (e) length method for class to determine length of string


void clear() ;                                                //// (g) clear method for class to clear content of the string


~MyString()                                           // (d) Destructor for class
{
  delete[] str;
}


};    // end of .h file content

Now .cpp content

#include<iostream>
#include "mystring.h"
//#include<string>

using std::cout;
using std::cin;


  
    MyString::MyString(const char *usrstr)                        // (b) a constructor that take c-MyString as argument
{
  //int usrstrlen=strlen(usrstr);
  
  int i=0;
  
  while(usrstr[i]!='')
  {
   i++;
  }
  
  int usrstrlen=i;
  this->size_str=usrstrlen;
  str=new char[size_str];
  //strcpy(str,usrstr);
  
  i=0;
  
  while(i<this->size_str)
  {
   this->str[i]=usrstr[i];
   i++;
     }
  
  }

MyString::MyString(MyString &usrstr)                             // (c) a copy constructor that take class own object as argument
{
  int i=0;
  this->size_str=usrstr.size_str;
  str=new char[this->size_str];
  
  while(i<this->size_str)
  {
   this->str[i]=usrstr.str[i];
   i++;
     }
    
    }


MyString& MyString::operator = (const MyString& usrstr)         // (i) Assignment operator overloaded to tackle situation like MyString s2=s1;
{
     //MyString newstr;
     int i=0;
  this->size_str=usrstr.size_str;
  
  this->str=new char[this->size_str];
  
  while(i<this->size_str)
  {
   this->str[i]=usrstr.str[i];
   i++;
     }
  
  return *this;
  }

MyString MyString::operator + (const MyString& usrstr1) //(h) + operator overloading

{

MyString myobj;

int i=0;

int j=0;

myobj.size_str= this->size_str + usrstr1.size_str;

myobj.str = new char[myobj.size_str];

while(i<this->size_str)

{

myobj.str[i]=this->str[i];

i++;

}

while(j<usrstr1.size_str)

{

myobj.str[i]=usrstr1.str[j];

i++;

j++;

}

cout<<myobj.str;

return myobj;

}

char* MyString::c_str() // function that return c style string to the calling function

{

char *cstring=new char[this->size_str+1];

int i=0;

while(i<this->size_str)

{

cstring[i]=this->str[i];

i++;

}

cstring[i]='';

return cstring;

}

int MyString::length()                                                // (e) length method for class to determine length of string
{
  return this->size_str;
  }


void MyString::clear()                                                 //// (g) clear method for class to clear content of the string
{
  int i=0;
  while(i<this->size_str)
  {
   this->str[i]='';
   i++;
    
  }
  
  this->size_str=0;
}

int main()

{

MyString obj1("hey");

MyString obj2(obj1);

MyString obj3=obj1;

MyString obj4;

MyString obj5;

MyString obj6;

fflush(stdin);

fflush(stdout);

cout<<obj1.str;

//cout<<" Second Object "<<obj2.str;

//cout<<" "<<obj2.length();

//obj2.clear();

cout<<" After clear Second Object length = ";

//cout<<" "<<obj2.str;

//cout<<obj2.length();

cout<<" After operator overloading";

cout<<" "<<obj3.str;

obj4="new";

obj5=obj3;

obj6=obj4+obj5;

cout<<" After operator overloading";

cout<<" "<<obj4.str;  

cout<<" "<<obj5.str;

cout<<" "<<obj6.str;

//char *cstring=new char[100];

//strcpy(cstring,obj6.c_str());

char *cstring=obj6.c_str();

cout<<cstring;

}

                                                      // End of .cpp content