Design and implement a class myString o Use a dynamic array

- Design and implement a class myString. o Use a dynamic array of char to store the characters of myString. o Use an int to keep track of the length of myString. o Provide a parameterized constructor that takes an initial string, a default constructor, destructor, assignment operator, and copy constructor. All need to correctly handle the dynamic array. o Also provide these member functions: length() //the length of myString isEqual() //if it is equal to another myString print() //output to a terminal cout or outFile reverse() //reverse the order of characters in myString isPalindrome() //if myString reads the same after being reversed Could someone please help me with this assignment?

Solution

//mystring.cpp
#include <iostream>
#include <string.h>
#include <stdlib.h>
using namespace std;

class myString {
   char *S;
   int len;
public:
myString() // A default constructor that sets up the String to be an empty string.
{
   len=0;
   S=NULL;
}
//destructor
~myString()
{
   free(S);
   len=0;
}
// assignment operator
void operator=(myString m)
{
len=m.length();
   S=(char*)malloc(len*sizeof(char));
   strcpy(S,m.S);
}
//copy constructor.
String (myString m)
{
   len=m.length();
   S=(char*)malloc(len*sizeof(char));
   strcpy(S,m.S);
}

int length() //the length of myString
{
   return len;
}

bool isEqual(myString m) //if it is equal to another myString
{
   if(strcmp(S,m.S)==0)
   {
       return true;
   }
   else
   return false;
}

void print() //output to a terminal cout
{
   cout<<\"String is \"<<S;
}

void reverse() //reverse the order of characters in myString
{
   S=strrev(S);
}

bool isPalindrome() //if myString reads the same after being reversed
{
   if(strcmp(S,(strrev(S))))
   {
       return true;
   }
   else
       return false;
}

};

- Design and implement a class myString. o Use a dynamic array of char to store the characters of myString. o Use an int to keep track of the length of myString
- Design and implement a class myString. o Use a dynamic array of char to store the characters of myString. o Use an int to keep track of the length of myString

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site