Create an Account

Already have account?

Forgot Your Password ?

Home / Questions / Write a program that implements a minimal "string" class. In this lab, you will be creating a...

Write a program that implements a minimal "string" class. In this lab, you will be creating a...

Write a program that implements a minimal "string" class. In this lab, you will be creating a class called "mystring" that will dynamically expand a string as more and more things are added to it. Remember that a string is nothing more than an array of characters. However, for that array to be contiguous, it must be recreated every time something is added to it.

 

mystring must support the following functions:

  1. Constructor - sets length to 0 and the internal pointer to nullptr.
  2. Constructor - takes a c-style array and copies it into the internal buffer.
  3. Copy Constructor - copies the data from the right-hand side. DO NOT COPY THE POINTER ITSELF!
  4. append - takes a character and adds it to the end of the string.
  5. append - takes a mystring and adds it to the end of the string.
  6. pop - removes the trailing character and shortens the string by 1. If after shortening the string, make sure you reset the length to 0 and delete any dynamic memory allocated.
  7. clear - deletes the string and sets the length to 0.
  8. at - takes an index and returns the character at that index. If the index is out of range, return '\0' (null).
  9. c_str - returns the internal buffer as a constant string. This function must ensure that the last character is '\0'. This is how c-style strings are "terminated". However, if the string is empty, return nullptr.
  10. length - returns the number of characters in the string.
  11. Destructor - essentially clears the string when it goes out of scope.
  12. A char * pointer to store the data.
  13. An integer length to store the number of characters in the string.

C-style strings

C-style strings are just characters in a contiguous array. Since we only know the starting point of the string, the last character is set to 0 to "terminate" it. So, we know the length of a string by counting all non-zero characters.

</pstyle="color:>

 

Apr 09 2020 View more View Less

Answer (Solved)

question Subscribe To Get Solution

Related Questions