C help thank you This project is to work on the assignment I

C++ help thank you!

This project is to work on the assignment I\'ve done before, I posted the previous assignment\'s requirement and my code for that one below so it might seems kind of lengthy......

(What we need to do)

you\'ll be making the following refinements to the class that you wrote in the last assignment. All the requirements from that class still apply. For example, all myStrings must always be stored in a dynamic array that is exactly the correct size to store the string.

Extraction Operator

Just like the >> operator that reads C-strings, your >> operator should skip any leading spaces and then read characters into the string up to the first whitespace character.

For reasons of convenience, we will impose a limit of 127 on the number of characters this function will read. This is so you can temporarily read into a non-dynamic array and then copy what you need into your data member, which will be a dynamic array. Note that this does not mean that all myStrings will always have a maximum of 127 characters. For example, you might get a myString with more than 127 characters by using the myString constructor or by concatenating two myStrings.

Hint: Don\'t try to read character by character in a loop. Use the extraction operator to do the reading of the input into a non-dynamic array, then use strcpy() to copy it into your data member. Make sure to allocate the correct amount of memory.

Hint: if you use the extraction operator as suggested above, you will not have to skip leading whitespace, because the extraction operator does that for you.

A read() function

The read() function will allow the client programmer to specify the delimiting character (the one to stop at instead of the first space). This will be a void function that will take two arguments, a stream and the delimiting character. It should not skip leading spaces. The limit of 127 characters imposed on the >> function above also applies to this function.

Hint: Don\'t try to read character by character in a loop. Use the in.getline() function to do the reading of the input into a non-dynamic array, then use strcpy() to copy it into your data member.

Concatenation Operator

Overload the + operator to do myString concatenation. The operator must be able to handle either myString objects or C-strings on either side of the operator. Be careful with the memory management here. You\'ll have to allocate enough memory to hold the new myString. I suggest using strcpy() to get the left operand into the result myString, and then strcat() to append the right operand. Both strcpy() and strcat() should be used as if they are void, even though they do have return values.

Combined Concatenation/Assignment Operator

Overload the shorthand += to combine concatenation and assignment. Only myStrings can be on the left-hand side of a += operation, but either myStrings or C-strings may appear on the right side. If you pay close attention to the += operator from the feetInches class, these may be the easiest points of the semester.

Add Documentation

Header File: In the case of a class, the header file should begin with a (typically) very large header comment. This comment should include a general description of the class (so a client programmer can tell right away whether she wants to use it), followed by a listing of all of the prototypes of public members, each with pre and post conditions. Note that this list of prototypes is still part of the comment. You will have to list the prototypes again in the code below this header comment! You are required to use pre/post conditions to document your public member functions. Do not include any comments regarding the implementation details in the header file! This very large comment will then be followed by the header file code (e.g. the class declaration), with no comments.

Implementation File: In the implementation file you should start with a class invariant. (I don\'t expect you to have prior knowledge of what a class invariant is. The description that follows should suffice.) The class invariant will include a description of the private data members and how they are used, as well as a statement of anything that you guarantee will always be true for class objects (for example: \"fraction objects will always be stored in lowest terms\"). Aside from the class invariant, the only comments you will need in your implementation file are comments on the implementation of complex functions, and comments on private functions (which do not get comments in the header file).

Comments in the header file will follow the following pattern.

Name your source code files mystring.h and mystring.cpp.

Data file:

Client program:

Correct output:

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

Previous work and requirement:

My work on this one:

myString.h

#ifndef myString_H
#define myString_H

#include
#include

using namespace std;

namespace cs_myString {
  
   class myString {
  
   public:
  

       /*
  
       Preconditon: Takes no parameter and return nothing.
       Postcondition: Initialize data member-\'desc\' with memory of size 1 and
       copy empty string to it
  
*/
       myString();
  
  
  
       /*
      
       Preconditon: Takes one argument - pointer to char object \'inDesc\' and
       is kept const
       Postcondition: object is created with size accordingly with specified
       object pointer given at input
  
       */
  
       myString(const char *inDesc);
  

  
  
      
       /*
  
       Preconditon: Takes one argument - reference to myString object \'right\'
       and is kept const
       Postcondition: object is created with size accordingly with specified
       object reference given at input
  
   */
       myString(const myString& right);
  
  
  

   /*
  
   Preconditon: an myString object exist with valid length
Postcondition: myString object is destroyed
  
   */
       ~myString();

  

  
   /*
  
Preconditon: Input argument contains address to myString object:-Right.
Postcondition: object is copied with size accordingly with specified
       object reference given at input
  
   */
myString operator=(const myString& right);

  


   /*
  
   Preconditon: Input argument contains address to myString object:-Right.
Postcondition: returns pointer to this object which was created by
       combination of concatenation and assignment operator
  
   */
myString operator+=(const myString& right);
  
  

       /*
  
Preconditon: Input argument contains pointer to char object:-Right.
Postcondition: returns pointer to this char object which was created by
       combination of concatenation and assignment operator
  
   */
myString operator+=(const char *right);

       /*
  
   Preconditon: length donot take any argument
Postcondition: returns length of myString object
  
   */
int length() const;


OR
       /*
       Precondition: The out of ostream object is waiting to receive the
       myString output.
Postcondition: myString source has been passed by reference to the
       ostream out and is kept const.
  
   */
friend ostream& operator<<(ostream& out, const myString& source);

       /*
  
   Precondition: The in of istream object is waiting to receive the
       myString input.
Postcondition: myString source has been passed by reference into
       the istream \'in\'.
  
   */
friend istream& operator>>(istream& in, myString &source);
  


       /*
  
   Precondition:- Input argument contains address to two myString object:
       - Left and Right.
Postcondition: function return\'s true, if Left < Right, else it return\'s
       false.
  
   */
friend bool operator<(const myString &Left, const myString &Right);
  
       /*
  
   Precondition:- Input parameters contains reference to two myString
       object:- Left and Right.
Postcondition: function return\'s true, if Left <= Right, else it
       return\'s false.
  
   */
friend bool operator<=(const myString &Left, const myString &Right);
  
       /*
  
   Precondition:- Input parameters contains reference to two myString
       object:- Left and Right.
Postcondition: function return\'s true, if Left == Right, else it
       return\'s false.
  
   */
friend bool operator==(const myString &Left, const myString &Right);
  
       /*
  
   Precondition:- Input parameters contains reference to two myString
       object:- Left and Right.
Postcondition: function return\'s true, if Left != Right, else it
       return\'s false.
  
   */
friend bool operator!=(const myString &Left, const myString &Right);
  
       /*
  
   Precondition:- Input parameters contains reference to two myString
       object:- Left and Right.
Postcondition: function return\'s true, if Left >= Right, else it
       return\'s false.
  
   */
friend bool operator>=(const myString &Left, const myString &Right);
  
       /*
  
   Precondition:- Input parameters contains reference to two myString
       object:- Left and Right.
Postcondition: function return\'s true, if Left > Right, else it
       return\'s false.
  
   */
friend bool operator>(const myString &Left, const myString &Right);
  

        /*
  
   Precondition:- Input argument contains address to myString object
       and c_string:- Left and Right.
Postcondition: function return\'s true, if Left < Right, else it
       return\'s false.
  
   */   

friend bool operator<(const myString &Left, const char* Right);
  
       /*
  
   Precondition:- Input parameters contains reference to myString object
       and c_string:- Left and Right.
Postcondition: function return\'s true, if Left <= Right, else it
       return\'s false.
  
   */
friend bool operator<=(const myString &Left, const char* Right);
  
       /*
  
   Precondition:- Input parameters contains reference to myString object
       and c_string:- Left and Right.
Postcondition: function return\'s true, if Left == Right, else it
       return\'s false.
  
   */
friend bool operator==(const myString &Left, const char* Right);
  
       /*
  
   Precondition:- Input parameters contains reference to myString object
       and c_string:- Left and Right.
Postcondition: function return\'s true, if Left != Right, else it
       return\'s false.
  
   */
friend bool operator!=(const myString &Left, const char* Right);
  
       /*
  
   Precondition:- Input parameters contains reference to myString object
       and c_string:- Left and Right.
Postcondition: function return\'s true, if Left >= Right, else it
       return\'s false.
  
   */
friend bool operator>=(const myString &Left, const char* Right);
  
       /*
  
   Precondition:- Input parameters contains reference to myString object
       and c_string:- Left and Right.
Postcondition: function return\'s true, if Left > Right, else it
       return\'s false.
  
   */
friend bool operator>(const myString &Left, const char* Right);
  
       /*
  
   Precondition:- Input argument contains address to c_string and myString
       object:- Left and Right.
Postcondition: function return\'s true, if Left < Right, else it return\'s
       false.
  
   */

friend bool operator<(const char* Left, const myString &Right);
  
       /*
  
   Precondition:- Input parameters contains reference to c_string and
       myString object:- Left and Right.
Postcondition: function return\'s true, if Left <= Right, else it
       return\'s false.
  
   */
friend bool operator<=(const char* Left, const myString &Right);
  
       /*
  
   Precondition:- Input parameters contains reference to c_string and
       myString object:- Left and Right.
Postcondition: function return\'s true, if Left == Right, else it
       return\'s false.
  
   */
friend bool operator==(const char* Left, const myString &Right);
  
       /*
  
   Precondition:- Input parameters contains reference to c_string and
       myString object:- Left and Right.
Postcondition: function return\'s true, if Left != Right, else it
       return\'s false.
  
   */
friend bool operator!=(const char* Left, const myString &Right);
  
       /*
  
   Precondition:- Input parameters contains reference to c_string and
       myString object:- Left and Right.
Postcondition: function return\'s true, if Left >= Right, else it
       return\'s false.
  
   */
friend bool operator>=(const char* Left, const myString &Right);
  
       /*
  
   Precondition:- Input parameters contains reference to c_string and
       myString object:- Left and Right.
Postcondition: function return\'s true, if Left > Right, else it
       return\'s false.
  
   */
friend bool operator>(const char* Left, const myString &Right);
  


        /*
  
   Preconditon: Input argument contains two argument, a stream and the
       delimiting character.
Postcondition: Reads character by character in a loop. Use the in.
       getline() function to do the reading of the input into a non-dynamic
       array, then use strcpy() to copy it into your data member. return void.
  
   */   
void read(istream& inString,char delim);
  
  

       /*
  
   Precondition:- Input parameters contains reference to two myString
       object:- Left and Right.
Postcondition: function return\'s addition of two myString objects.
  
   */   
friend const myString operator+(const myString &left, const myString &right);
  
  
  

       /*
  
   Preconditon: Input argument contains value of index.
Postcondition: Returns the index to myString specifying the char to
       that location which is const.
  
   */
char operator[](int index) const;
  
  
       /*
  
   Preconditon: Input argument contains value of index.
Postcondition: Returns the index to myString specifying the char to
       that location.
  
   */
char& operator[](int index);
  
  
  
  
private:
  
char *desc;

};

  
}
#endif

myString.cpp

#include
#include \"myString.h\"

using namespace std;


namespace cs_myString {

/*
This part initialize desc with memory of size 1 and copy empty string to it.
*/
  
  
myString::myString() {
  
desc = new char[1];
strcpy(desc, \"\");
}
/*
The parameterized constructor takes pointer argument pointing to the object.

*/
  
myString::myString(const char *inDesc) {
  
desc = new char[strlen(inDesc) + 1];
strcpy(desc, inDesc);
}
/*
The parameterized construtor takes one reference parameter the address to the
object location.
*/
  
myString::myString(const myString& right) {
  
desc = new char[strlen(right.desc) + 1];
strcpy(desc, right.desc);
}


myString::~myString() {
  
delete [] desc;
}
/*
This part has the overloaded operator = that takes one argument reference to
object and is kept const.

*/
  
  
myString myString::operator=(const myString& right) {
  
if (this != &right) {
  
delete [] desc;
desc = new char[strlen(right.desc) + 1];
strcpy(desc, right.desc);
}
  
return *this;
}
/*

These parts overloaded operators, and are able to compare myString objects to
other myStrings as well as myStrings to c-strings


*/
  
bool operator==(const myString& left, const myString& right) {
  
return strcmp(left.desc, right.desc) == 0;
}


bool operator==(const myString& left, const char* right) {

return strcmp(left.desc, right) == 0;
}


bool operator==(const char* left, const myString& right) {

return strcmp(left, right.desc) == 0;
}

  
bool operator<(const myString& left, const myString& right) {
  
return strcmp(left.desc, right.desc) < 0;
}


bool operator<(const myString& left, const char* right) {

return strcmp(left.desc, right) < 0;
}


bool operator<(const char* left, const myString& right) {

return strcmp(left, right.desc) < 0;
}

  
bool operator!=(const myString& left, const myString& right) {
  
return !(left == right);
}


bool operator!=(const myString& left, const char* right) {

return !(left == right);
}


bool operator!=(const char* left, const myString& right) {

return !(left == right);
}

  
bool operator>(const myString& left, const myString& right) {
  
return !(left == right) && !(left < right);
}


bool operator>(const myString& left, const char* right) {

return !(left == right) && !(left < right);
}


bool operator>(const char* left, const myString& right) {

return !(left == right) && !(left < right);
}

  
bool operator<=(const myString& left, const myString& right) {
  
return left < right || left == right;
}


bool operator<=(const myString& left, const char* right) {

return left < right || left == right;
}


bool operator<=(const char* left, const myString& right) {

return left < right || left == right;
}

  
bool operator>=(const myString& left, const myString& right) {
  
return !(left < right);
}


bool operator>=(const myString& left, const char* right) {

return !(left < right);
}


bool operator>=(const char* left, const myString& right) {

return !(left < right);
}
/*

This part return length of string in int.


*/
int myString::length() const {
  
return (int) strlen(desc);
}
/*

This part overloaded operator << function takes computed address of source and
is passed to out object of ostream to display results.


*/

ostream& operator<<(ostream& out, const myString& source) {
  
out << source.desc;
return out;
}
/*
This part overload The Square Brackets returns a const char for the index returned.
*/

char myString::operator[](int index) const {
  
assert(index >= 0 && index < strlen(desc));
return desc[index];
}
/*
This part overload The Square Brackets returns an reference to char at that
index.
*/
char& myString::operator[](int index) {
  
assert(index >= 0 && index < strlen(desc));
return desc[index];
}
/*
This part overloaded Extraction >> operator function takes address of source.
*/
istream& operator>>(istream& in, myString &source)
{
  
char temp[127];
delete [] source.desc;
in >> temp;
source.desc = new char[strlen(temp) + 1 ];
strcpy(source.desc, temp);
return in;
}
/*
The read() function returns void and takes two arguments, a stream and
the delimiting character. It does not skip leading spaces.
*/
  
void myString::read(istream &source, char delim)
{
char temp[127];
delete [] desc;
source.getline(temp,127,delim);
desc = new char[strlen(temp) + 1 ];
strcpy(desc,temp);
}
/*
This part overloaded operator + function, takes two parameter which is reference to
myString \'left\' & \'right\' and is declared const.
*/
const myString operator+(const myString &left, const myString &right)
{
  
myString temp;
temp = new char[strlen(left.desc) + strlen(right.desc) + 1];
strcpy(temp.desc, left.desc);
strcat(temp.desc, right.desc);
return temp;
}
/*
This part overloaded the shorthand += to combine concatenation and assignment.

*/
myString myString::operator+=(const myString& right)
{
  
myString temp;
temp = new char[strlen(right.desc) + 1];
delete [] desc;
desc = new char[strlen(temp.desc) + strlen(right.desc) + 1];
strcat(desc, right.desc);
return *this;
  
}
/*
This part overloaded the shorthand += to combine concatenation and assignment.

*/
myString myString::operator+=(const char *right)
{
myString temp;
temp = new char[strlen(right) + 1];
delete [] desc;
desc = new char[strlen(temp.desc) + strlen(right) + 1];
strcat(desc, right);
return *this;
}
}

See also client program and correct output.

Clint program

Correct output

Requirements:

Write a string class. To avoid conflicts with other similarly named classes, we will call our version myString. This object is designed to make working with sequences of characters a little more convenient and less error-prone than handling raw c-strings, (although it will be implemented as a c-string behind the scenes). The myString class will handle constructing strings, reading/printing, and accessing characters. In addition, the myString object will have the ability to make a full deep-copy of itself when copied.

Your class must have only one data member, a c-string implemented as a dynamic array. In particular, you must not use a data member to keep track of the size or length of the myString.

This is the first part of a two part assignment. In the next assignment you will be making some refinements to the class that you create in this assignment. For example, no documentation is required this week, but full documentation will be required next week.

Here is a list of the operations this class must support:

A length member function which returns the number of characters in the string. Use strlen().

Construction of a myString from a const c-string. You should copy the string data, not just store a pointer to an argument passed to the constructor. Constructing a myString with no arguments creates an empty myString object (i.e. \"\"). A myString object should be implemented efficiently (space-wise) which is to say you should not have a fixed-size buffer of chars, but instead allocate space for chars on an as-needed basis. Use strcpy().

Printing a myString to a stream using an overloaded << (insertion) operator, which should simply print out its characters. Use <<.

Your myString object should overload the square brackets [ ] operator to allow direct access to the individual characters of the string. This operation should range-check and assert if the index is out of bounds. You will write two versions of the [ ] operator, a const version that allows read access to the chars, and a non-const version that returns the client a reference to the char so they can change the value.

All six of the relational operators (<, <=, >, >=, ==, !=) should be supported. They should be able to compare myString objects to other myStrings as well as myStrings to c-strings. The ordering will be based on ASCII values. You can think of this as essentially alphabetical order; however, because of the way that ASCII values are defined, uppercase letters will always come before lowercase letters, and punctuation will make things even more complicated. Confused? You don\'t need to worry about any of this: just use the results of calling the strcmp() function. myStrings or c-strings should be able to appear on either side of the comparison operator.

Of course, you\'ll also need to include the four functions discussed in the lesson that are always required in classes that use dynamic memory. (These functions, excluding the default constructor, are otherwise known as the \"big-three\".)

You may use all of the c-string functionality provided by C++. This will include the strlen(), strcmp(), and strcpy() functions, along with the overloaded insertion operator for c-strings. These functions are all covered in detail in the text. When you use strcpy() treat it as a void function despite the fact that it has a return value. Do not use strncpy(), strncat(), or strncmp() since they are not implemented in all versions of C++. You may NOT use anything from the C++ string class!!

You must place your header file and implementation file in a namespace. Normally one would call a namespace something more likely to be unique, but for purposes of convenience we will call our namespace \"cs_mystring\".

Solution

Solution:

Note: Solution is prepared by modifying given code.

The C++ program code is shown below:

//MyString.h

//Include header files

#ifndef MyString_h

#define MyString_h

#include <iostream>

//Define cs_mystring

namespace cs_mystring

{

     //Define class MyString

     class MyString

     {

          //Declare variable

          private:

          char *desc;

          /*Function prototypes*/

          public:

          MyString();

          MyString(const char *inDesc);

          //MyString()

          MyString(const MyString &right);

          ~MyString();

          char operator[](int index)const;

          char& operator[](int index);

          long length()const;

friend std::ostream& operator<<(std::ostream& out, const MyString& myString);

          MyString operator=(const MyString &right);

friend bool operator>(const MyString &left, const MyString &right);

friend bool operator<(const MyString &left, const MyString &right);

friend bool operator>=(const MyString &left, const MyString &right);

friend bool operator<=(const MyString &left, const MyString &right);

friend bool operator==(const MyString &left, const MyString &right);

friend bool operator!=(const MyString &left, const MyString &right);

friend std::istream& operator>>(std::istream& in, MyString& target);

friend MyString operator+(const MyString& left, const MyString& right);

          void read(std::istream& in, char delimiter);

          MyString operator+=(const MyString& right);

     };

}

#endif

//myString.cpp

//Include libraries

#include <stdio.h>

#include <stdlib.h>

#include <cstring>

#include <cassert>

#include \"MyString.h\"

using namespace std;

//Define cs_mystring

namespace cs_mystring

{

     //Define cMyString()

   MyString::MyString()

   {

        //Set desc

      desc = new char[1];

     //Call strcpy()

      strcpy(desc, \"\");

   }

   //Define MyString()

   MyString::MyString(const char *inDesc)

   {

        //Set desc

      desc = new char[strlen(inDesc) + 1];

     //Call strcpy()

      strcpy(desc, inDesc);

   }

   //Define MyString()

   MyString::MyString(const MyString& right)

   {

        //Set desc

      desc = new char[strlen(right.desc) + 1];

     

     //Call strcpy()

     strcpy(desc, right.desc);

   }

   //Define destructor

   MyString::~MyString()

   {

        //Call delete operation

      delete [] desc;

   }

//Define operator=()

   MyString MyString::operator=(const MyString &right)

   {

        //If this not equals &right

      if (this != &right)

      {

          //Call delete operation

         delete [] desc;

          //Set desc

         desc = new char[strlen(right.desc) + 1];

        

          //Call strcpy

          strcpy(desc, right.desc);

      }

     //Return

      return *this;

   }

   //Define operator[]

   char MyString::operator[](int index)const

   {

        //Call assert()

      assert(index >= 0 && index < strlen(desc));

     

     //Return desc[]

     return desc[index];

   }

   //Define operator[]

   char& MyString::operator[](int index)

   {

        //Call assert

      assert(index >= 0 && index < strlen(desc));

     

     //Return desc[]

     return desc[index];

   }

   //Define length()

   long MyString::length()const

   {

        //Set length

      long length = strlen(desc);

     

     //Return length

     return length;

   }

//Define operator<<()

   ostream& operator<<(ostream& out, const MyString& myString)

   {

        //Write

      out << myString.desc;

     

     //Return out

     return out;

   }

   //Define operator>()

   bool operator>(const MyString &left, const MyString &right)

   {

        //If left is greater than right

      if(strcmp(left.desc, right.desc) > 0)

        

          //Return true

          return true;

     //Return false

      return false;

   }

//Define operator<()

   bool operator<(const MyString &left, const MyString &right)

   {

        //If left is less than right

      if(strcmp(left.desc, right.desc) < 0)

          //Return true

         return true;

     //Otherwise return false

      return false;

   }

   //Define operator>=()

   bool operator>=(const MyString &left, const MyString &right)

   {

        //If left greater than or equals right

      if(strcmp(left.desc, right.desc) >= 0)

          //Return true

         return true;

     //Otherwise return false

      return false;

   }

   //Define operator<=()

   bool operator<=(const MyString &left, const MyString &right)

   {

        //If left less than or equal to 0

      if(strcmp(left.desc, right.desc) <= 0)

        

          //Return true

          return true;

     //Otherwise return false

      return false;

   }

//Define operator==()

   bool operator==(const MyString &left, const MyString &right)

   {

        //If left equals right

      if(strcmp(left.desc, right.desc) == 0)

        

          //Return true

          return true;

     

     //Otherwise return false

     return false;

   }

//Define operator!=()

   bool operator!=(const MyString &left, const MyString &right)

   {

        //If left not equals right

      if(strcmp(left.desc, right.desc) != 0)

        

          //Return true

          return true;

     //Return false

      return false;

   }

   //Define operator>>()

   istream& operator>>(istream& in, MyString& target)

   {

      //Loop until input

      while (isspace(in.peek()))

     {

          //call ignore()

         in.ignore();

      }

     //Define array

      char temp[128];

     //Call getline()

      in.getline(temp, 127, \' \');

     

     //Call delete operation

     delete [] target.desc;

     //Create new array

      target.desc = new char[strlen(temp) +1];

     

     //Call strcpy()

     strcpy(target.desc, temp);

     //Return in

      return in;

   }

  

   //Define operator+()

   MyString operator+(const MyString& left, const MyString& right)

   {

        //Define strLength

      long strLength = strlen(left.desc);

     

     //Add length of right

     strLength += strlen(right.desc);

     //Define temp

      MyString temp = new char[strLength + 1];

     //Call strcpy()

      strcpy(temp.desc, left.desc);

     

     //Call strcat()

     strcat(temp.desc, right.desc);

     //Return temp

      return temp;

   }

   //Define read()

   void MyString::read(std::istream& in, char delimiter)

   {

        //Define array

      char temp[128];

     //Call getline()

      in.getline(temp, 127, \'\ \');

     //Call delete operation

      delete [] this->desc;

     //Create new array

      this->desc = new char[strlen(temp) +1];

     

     //Call strcpy()

     strcpy(this->desc, temp);

   }

//Define operator+=()

   MyString MyString::operator+=(const MyString& right)

   {

        //Define strLength

      long strLength = strlen(this->desc) + strlen(right.desc) + 1;

     

     //Define temp

     MyString temp = new char[strLength];

     //Call strcpy()

      strcpy(temp.desc, this->desc);

     //Call delete operation

      delete [] this->desc;

     //Create new array

      this->desc = new char[strLength];

     //Call strcpy()

      strcpy(this->desc, temp.desc);

     

     //Call strcat()

     strcat(this->desc, right.desc);

     //Return

      return *this;

   }

}

//Main.cpp

//Include libraries

#include \"MyString.h\"

#include <fstream>

#include <cctype>     

#include <string>

#include <cassert>

#include <iostream>

using namespace std;

using namespace cs_mystring;

/*Function prototypes */

bool eof(istream& in);

void BasicTest();

void RelationTest();

void ConcatTest();

void CopyTest();

MyString AppendTest(const MyString& ref, MyString val);

string boolString(bool convertMe);

//Define main method

int main()

{

   //Call BasicTest()

   BasicTest();

   //Call RelationTest()

   RelationTest();

   //Call ConcatTest()

   ConcatTest();

   //Call CopyTest()

   CopyTest();

   //Pause console window

   system(\"pause\");

   //Return 0

   return 0;

}

//Define eof()

bool eof(istream& in)

{

     //Define ch

   char ch;

   //Store ch

   in >> ch;

   //Call putback()

   in.putback(ch);

   //Return

   return !in;

}

//Define boolString()

string boolString(bool convertMe)

{

  

     //If convertMe

     if (convertMe)

     {

          //Return true

      return \"true\";

   }

     //Otherwise

     else

     {

          //Return false

      return \"false\";

   }

}

//Define BasicTest()

void BasicTest()

{

     //Define s

   MyString s;

   //Display message

cout << \"----- Testing basic String creation & printing\" << endl;

   //Define strings

   const MyString strs[] =

   {MyString(\"Wow\"), MyString(\"C++ is neat!\"),

      MyString(\"\"), MyString(\"a-z\")};

     //Loop until 4

   for (int i = 0; i < 4; i++)

   {

        //Display string

      cout << \"string [\" << i <<\"] = \" << strs[i] << endl;

   }

   //Display message

cout << endl << \"----- Now reading MyStrings from file\" << endl;

   cout << endl << \"----- first, word by word\" << endl;

   //in()

   ifstream in(\"mystring.txt\");

  

   //Call assert()

   assert(in);

   //Loop until # is seen

   while (in.peek() == \'#\')

   {

        //Call ignore()

      in.ignore(128, \'\ \');

   }

   //Store s

   in >> s;

   //Loop until length

   while (in)

   {

        //Display message

      cout << \"Read string = \" << s << endl;

     

     //Store s

     in >> s;

   }

   //Close

   in.close();

   //Display message

   cout << endl << \"----- now, line by line\" << endl;

  

   //Tesx file

   ifstream in2(\"mystring.txt\");

  

   //Call assert()

   assert(in2);

   //Loop until # is seen

   while (in2.peek() == \'#\')

   {

        //Call ignore()

      in2.ignore(128, \'\ \');

   }

   //Call read()

   s.read(in2, \'\ \');

  

   //Loop until length

   while (in2)

   {

        //Display message

      cout << \"Read string = \" << s << endl;

     

     //Call read()

     s.read(in2, \'\ \');

   }

   //Display message

cout << endl << \"----- Testing access to characters (using const)\" << endl;

  

   //Define s1

   const MyString s1(\"abcdefghijklmnopqsrtuvwxyz\");

  

   //Display string s1

   cout << \"Whole string is \" << s1 << endl;

  

   //Display string by each character

   cout << \"now char by char: \";

   //Loop until length

   for (int i = 0; i < s1.length(); i++)

   {

        //Display character

      cout << s1[i];

   }

   //Display message

cout << endl << \"----- Testing access to characters (using non-const)\" << endl;

   //Define string s2

   MyString s2(\"abcdefghijklmnopqsrtuvwxyz\");

   //Display message

   cout << \"Start with \" << s2;

  

   //Loop until length

   for (int i = 0; i < s2.length(); i++)

   {

        //Change to upper case

      s2[i] = toupper(s2[i]);

   }

   //Display string s2

   cout << \" and convert to \" << s2 << endl;

}

//Loop until RelationTest()

void RelationTest()

{

     //Display message

   cout << \"\ ----- Testing relational operators between MyStrings\ \";

   //Define string

   const MyString strs[] =

   {MyString(\"app\"), MyString(\"apple\"), MyString(\"\"),

      MyString(\"Banana\"), MyString(\"Banana\")};

   //Loop until 4

   for (int i = 0; i < 4; i++) {

        //Display message

cout << \"Comparing \" << strs[i] << \" to \" << strs[i+1] << endl;

     //Test operator <

cout << \"\\tIs left < right? \" << boolString(strs[i] < strs[i+1]) << endl;

     

     //Test operator <=

cout << \"\\tIs left <= right? \" << boolString(strs[i] <= strs[i+1]) << endl;

     

     //Test operator >

cout << \"\\tIs left > right? \" << boolString(strs[i] > strs[i+1]) << endl;

     

     //Test operator >=

cout << \"\\tIs left >= right? \" << boolString(strs[i] >= strs[i+1]) << endl;

     

     //Test operator ==

cout << \"\\tDoes left == right? \" << boolString(strs[i] == strs[i+1]) << endl;

     

     //Test operator !=

     cout << \"\\tDoes left != right ? \" << boolString(strs[i] != strs[i+1]) << endl;

   }

//Display message

   cout << \"\ ----- Testing relations between MyStrings and char *\ \";

  

   //Define string s

   MyString s(\"he\");

   //Define *t

   const char *t = \"hello\";

  

   //Display message

   cout << \"Comparing \" << s << \" to \" << t << endl;

   cout << \"\\tIs left < right? \" << boolString(s < t) << endl;

   cout << \"\\tIs left <= right? \" << boolString(s <= t) << endl;

   cout << \"\\tIs left > right? \" << boolString(s > t) << endl;

   cout << \"\\tIs left >= right? \" << boolString(s >= t) << endl;

   cout << \"\\tDoes left == right? \" << boolString(s == t) <<    endl;

cout << \"\\tDoes left != right ? \" << boolString(s != t) << endl;

   //Define string u

   MyString u(\"wackity\");

   //Define v

   const char *v = \"why\";

   //Display message

   cout << \"Comparing \" << v << \" to \" << u << endl;

   //Display result

   cout << \"\\tIs left < right? \" << boolString(v < u) << endl;

  

   //Display result

   cout << \"\\tIs left <= right? \" << boolString(v <= u) << endl;

  

   //Display result

   cout << \"\\tIs left > right? \" << boolString(v > u) << endl;

  

   //Display result

   cout << \"\\tIs left >= right? \" << boolString(v >= u) << endl;

  

   //Display result

   cout << \"\\tDoes left == right? \" << boolString(v == u) << endl;

  

   //Display result

   cout << \"\\tDoes left != right ? \" << boolString(v != u) << endl;

}

//Define ConcatTest()

void ConcatTest()

{

     //Display message

   cout << \"\ ----- Testing concatentation on MyStrings\ \";

   //Define s

   const MyString s[] =

   {MyString(\"outrageous\"), MyString(\"milk\"), MyString(\"\"),

      MyString(\"cow\"), MyString(\"bell\")};

   //Loop until 4

   for (int i = 0; i < 4; i++)

   {

     

        //Display string

        cout << s[i] << \" + \" << s[i+1] << \" = \" << s[i] + s[i+1] << endl;

   }

   //Display message

   cout << \"\ ----- Testing concatentation between MyString and char *\ \";

   //Define a

   const MyString a(\"abcde\");

  

   //Define b

   const char *b = \"XYZ\";

  

   //Test a+b

   cout << a << \" + \" << b << \" = \" << a + b << endl;

  

   //Test b+a

   cout << b << \" + \" << a << \" = \" << b + a << endl;

   //Display message

   cout << \"\ ----- Testing shorthand concat/assign on MyStrings\ \";

//Define s2[]

   MyString s2[] =

   {MyString(\"who\"), MyString(\"what\"), MyString(\"WHEN\"),

      MyString(\"Where\"), MyString(\"why\")};

   //Loop until 4

   for (int i = 0; i < 4; i++)

   {

        //Test operator +=

      cout << s2[i] << \" += \" << s2[i+1] << \" = \";

     

     //Display result

     cout << (s2[i] += s2[i+1]) << \"and\";

     

     //Display result

     cout << s2[i] << endl;

   }

   //Display message

   cout << \"\ ----- Testing shorthand concat/assign using char *\ \";

  

   //Define u

   MyString u(\"I love \");

  

   //Define v

   const char *v = \"programming\";

  

   //Display result

   cout << u << \" += \" << v << \" = \";

   //Display result

   cout << (u += v) << endl;

}

//Define AppendTest()

MyString AppendTest(const MyString& ref, MyString val)

{

     //Define val[]

   val[0] = \'B\';

   //Return value

   return val + ref;

}

//Define CopyTest()

void CopyTest()

{

     //Display message

   cout << \"\ ----- Testing copy constructor and operator= on MyStrings\ \";

   //Define orig()

   MyString orig(\"cake\");

//Define copy()

   MyString copy(orig);   

   //Change first letter

   copy[0] = \'f\';

   //Display result

   cout << \"original is \" << orig << \", copy is \" << copy << endl;

   //Define copy2

   MyString copy2;     

// Invoke operator=

   copy2 = orig;

   // Change first letter of *copy*

   copy2[0] = \'f\';

   //Display result

   cout << \"original is \" << orig << \", copy is \" << copy2 << endl;

   //Set copy2

   copy2 = \"Copy Cat\";

  

   //Copy onto self

   copy2 = copy2;     

   //Display result

   cout << \"after self assignment, copy is \" << copy2 << endl;

//Display message

   cout << \"Testing pass & return MyStrings by value and ref\" << endl;

  

   //Define val

   MyString val = \"winky\";

  

   //Define sum

   MyString sum = AppendTest(\"Boo\", val);

   //Display message

   cout << \"after calling Append, sum is \" << sum << endl;

  

   //Display result

   cout << \"val is \" << val << endl;

  

   //Set sum to val

   val = sum;

   //Display result

   cout << \"after assign, val is \" << val << endl;

}

Sample Output:

----- Testing basic String creation & printing

string [0] = Wow

string [1] = C++ is neat!

string [2] =

string [3] = a-z

----- Now reading MyStrings from file

----- first, word by word

Read string = The

Read string = first

Read string = time

Read string = we

Read string = will

Read string = read

Read string = individual

Read string = words,

Read string = next

Read string = we

Read string = read

Read string = whole

Read string = lines

----- now, line by line

Read string = The first time we will

Read string =     read individual words, next

Read string = we read whole lines

----- Testing access to characters (using const)

Whole string is abcdefghijklmnopqsrtuvwxyz

now char by char: abcdefghijklmnopqsrtuvwxyz

----- Testing access to characters (using non-const)

Start with abcdefghijklmnopqsrtuvwxyz and convert to ABCDEFGHIJKLMNOPQSRTUVWXYZ

----- Testing relational operators between MyStrings

Comparing app to apple

        Is left < right? true

        Is left <= right? true

        Is left > right? false

        Is left >= right? false

        Does left == right? false

        Does left != right ? true

Comparing apple to

        Is left < right? false

        Is left <= right? false

        Is left > right? true

        Is left >= right? true

        Does left == right? false

        Does left != right ? true

Comparing to Banana

        Is left < right? true

        Is left <= right? true

        Is left > right? false

        Is left >= right? false

        Does left == right? false

        Does left != right ? true

Comparing Banana to Banana

        Is left < right? false

        Is left <= right? true

        Is left > right? false

        Is left >= right? true

        Does left == right? true

        Does left != right ? false

----- Testing relations between MyStrings and char *

Comparing he to hello

        Is left < right? true

        Is left <= right? true

        Is left > right? false

        Is left >= right? false

        Does left == right? false

        Does left != right ? true

Comparing why to wackity

        Is left < right? false

        Is left <= right? false

        Is left > right? true

        Is left >= right? true

        Does left == right? false

        Does left != right ? true

----- Testing concatentation on MyStrings

outrageous + milk = outrageousmilk

milk + = milk

+ cow = cow

cow + bell = cowbell

----- Testing concatentation between MyString and char *

abcde + XYZ = abcdeXYZ

XYZ + abcde = XYZabcde

----- Testing shorthand concat/assign on MyStrings

who += what = whowhatandwhowhat

what += WHEN = whatWHENandwhatWHEN

WHEN += Where = WHENWhereandWHENWhere

Where += why = WherewhyandWherewhy

----- Testing shorthand concat/assign using char *

I love += programming = I love programming

----- Testing copy constructor and operator= on MyStrings

original is cake, copy is fake

original is cake, copy is fake

after self assignment, copy is Copy Cat

Testing pass & return MyStrings by value and ref

after calling Append, sum is BinkyBoo

val is winky

after assign, val is BinkyBoo

Press any key to continue .

C++ help thank you! This project is to work on the assignment I\'ve done before, I posted the previous assignment\'s requirement and my code for that one below
C++ help thank you! This project is to work on the assignment I\'ve done before, I posted the previous assignment\'s requirement and my code for that one below
C++ help thank you! This project is to work on the assignment I\'ve done before, I posted the previous assignment\'s requirement and my code for that one below
C++ help thank you! This project is to work on the assignment I\'ve done before, I posted the previous assignment\'s requirement and my code for that one below
C++ help thank you! This project is to work on the assignment I\'ve done before, I posted the previous assignment\'s requirement and my code for that one below
C++ help thank you! This project is to work on the assignment I\'ve done before, I posted the previous assignment\'s requirement and my code for that one below
C++ help thank you! This project is to work on the assignment I\'ve done before, I posted the previous assignment\'s requirement and my code for that one below
C++ help thank you! This project is to work on the assignment I\'ve done before, I posted the previous assignment\'s requirement and my code for that one below
C++ help thank you! This project is to work on the assignment I\'ve done before, I posted the previous assignment\'s requirement and my code for that one below
C++ help thank you! This project is to work on the assignment I\'ve done before, I posted the previous assignment\'s requirement and my code for that one below
C++ help thank you! This project is to work on the assignment I\'ve done before, I posted the previous assignment\'s requirement and my code for that one below
C++ help thank you! This project is to work on the assignment I\'ve done before, I posted the previous assignment\'s requirement and my code for that one below
C++ help thank you! This project is to work on the assignment I\'ve done before, I posted the previous assignment\'s requirement and my code for that one below
C++ help thank you! This project is to work on the assignment I\'ve done before, I posted the previous assignment\'s requirement and my code for that one below
C++ help thank you! This project is to work on the assignment I\'ve done before, I posted the previous assignment\'s requirement and my code for that one below
C++ help thank you! This project is to work on the assignment I\'ve done before, I posted the previous assignment\'s requirement and my code for that one below
C++ help thank you! This project is to work on the assignment I\'ve done before, I posted the previous assignment\'s requirement and my code for that one below
C++ help thank you! This project is to work on the assignment I\'ve done before, I posted the previous assignment\'s requirement and my code for that one below
C++ help thank you! This project is to work on the assignment I\'ve done before, I posted the previous assignment\'s requirement and my code for that one below
C++ help thank you! This project is to work on the assignment I\'ve done before, I posted the previous assignment\'s requirement and my code for that one below
C++ help thank you! This project is to work on the assignment I\'ve done before, I posted the previous assignment\'s requirement and my code for that one below
C++ help thank you! This project is to work on the assignment I\'ve done before, I posted the previous assignment\'s requirement and my code for that one below
C++ help thank you! This project is to work on the assignment I\'ve done before, I posted the previous assignment\'s requirement and my code for that one below
C++ help thank you! This project is to work on the assignment I\'ve done before, I posted the previous assignment\'s requirement and my code for that one below
C++ help thank you! This project is to work on the assignment I\'ve done before, I posted the previous assignment\'s requirement and my code for that one below
C++ help thank you! This project is to work on the assignment I\'ve done before, I posted the previous assignment\'s requirement and my code for that one below
C++ help thank you! This project is to work on the assignment I\'ve done before, I posted the previous assignment\'s requirement and my code for that one below
C++ help thank you! This project is to work on the assignment I\'ve done before, I posted the previous assignment\'s requirement and my code for that one below
C++ help thank you! This project is to work on the assignment I\'ve done before, I posted the previous assignment\'s requirement and my code for that one below
C++ help thank you! This project is to work on the assignment I\'ve done before, I posted the previous assignment\'s requirement and my code for that one below
C++ help thank you! This project is to work on the assignment I\'ve done before, I posted the previous assignment\'s requirement and my code for that one below
C++ help thank you! This project is to work on the assignment I\'ve done before, I posted the previous assignment\'s requirement and my code for that one below
C++ help thank you! This project is to work on the assignment I\'ve done before, I posted the previous assignment\'s requirement and my code for that one below
C++ help thank you! This project is to work on the assignment I\'ve done before, I posted the previous assignment\'s requirement and my code for that one below
C++ help thank you! This project is to work on the assignment I\'ve done before, I posted the previous assignment\'s requirement and my code for that one below

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site