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

Requirements: Write a string class. To avoid conflicts with other similarly name

ID: 3825147 • Letter: R

Question

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.

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.

Don't forget to include the big-3.

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!!

Unfortunately, Visual C++ will, under its default settings, report an error when you try to use strcpy() or strcat(), even though they are standard C++. You can prevent this by adding this line as the first line in your file:

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".

Client Program:

Correct Output:

Explanation / Answer

myString.h

#include <iostream>

namespace cs_mystring {

class myString {

char* m_str;

public:

myString(const myString& src);

myString(const char* src);

myString();

~myString();

const char* c_str() const;

int length() const;

myString& operator = (const myString& src);

char operator [] (int index) const;

char& operator [] (int index);

bool operator < (const myString& src) const;

bool operator <= (const myString& src) const;

bool operator > (const myString& src) const;

bool operator >= (const myString& src) const;

bool operator == (const myString& src) const;

bool operator != (const myString& src) const;

bool myString::operator < (const char* src) const;

bool myString::operator <= (const char* src) const;

bool myString::operator > (const char* src) const;

bool myString::operator >= (const char* src) const;

bool myString::operator == (const char* src) const;

bool myString::operator != (const char* src) const;

void read(std::ifstream& in, char endOfCharh);

myString operator+(const myString &rhs)const;

myString operator+(const char *rhs)const;

myString& operator+=(const myString &rhs);

myString& operator+=(const char *rhs);

};

bool operator < (const char* s1, const myString& s2);

bool operator <= (const char* s1, const myString& s2);

bool operator > (const char* s1, const myString& s2);

bool operator >= (const char* s1, const myString& s2);

bool operator == (const char* s1, const myString& s2);

bool operator != (const char* s1, const myString& s2);

std::ostream& operator << (std::ostream& strm, const myString& str);

std::ifstream& operator >> (std::ifstream& in, myString& str);

char* operator+(const char* lhs, const myString &rhs);

}

myString.cpp

#define _CRT_SECURE_NO_WARNINGS

#include "myString.h"

#include <ostream>

#include <cstring>

#include <cassert>

#include <fstream>

using namespace std;

using namespace cs_mystring;

myString::myString(const myString& src)

: m_str(new char[src.length() + 1])

{

strcpy(m_str, src.c_str());

}

myString::myString(const char* src)

{

if (src)

{

m_str = new char[strlen(src) + 1];

strcpy(m_str, src);

}

else

{

m_str = 0;

}

}

myString::myString()

: M_str (0)

{

}

myString::~myString()

{

delete[] m_str;

}

const char* myString::c_str() const

{

return m_str;

}

int myString::length() const

{

if (m_str == 0) {

return 0;

}

return (int)strlen(m_str);

}

myString& myString::operator = (const myString& src)

{

if (src != *this)

{

delete m_str;

m_str = new char[src.length() + 1];

strcpy(m_str, src.c_str());

}

return *this;

}

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

{

assert(0 <= index && index < length());

return m_str[index];

}

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

{

assert(0 <= index && index < length());

return m_str[index];

}

bool myString::operator < (const myString& src) const { return strcmp(m_str, src.c_str()) < 0; }

bool myString::operator <= (const myString& src) const { return strcmp(m_str, src.c_str()) <= 0; }

bool myString::operator > (const myString& src) const { return strcmp(m_str, src.c_str()) > 0; }

bool myString::operator >= (const myString& src) const { return strcmp(m_str, src.c_str()) >= 0; }

bool myString::operator == (const myString& src) const

{

if (!m_str || !src.c_str()) {

return false;

}

return strcmp(m_str, src.c_str()) == 0;

}

bool myString::operator != (const myString& src) const

{

return !(*this==src);

}

bool myString::operator < (const char* src) const { return strcmp(m_str, src) < 0; }

bool myString::operator <= (const char* src) const { return strcmp(m_str, src) <= 0; }

bool myString::operator > (const char* src) const { return strcmp(m_str, src) > 0; }

bool myString::operator >= (const char* src) const { return strcmp(m_str, src) >= 0; }

bool myString::operator == (const char* src) const { return strcmp(m_str, src) == 0; }

bool myString::operator != (const char* src) const { return strcmp(m_str, src) != 0; }

bool cs_mystring::operator < (const char* s1, const cs_mystring::myString& s2) { return strcmp(s1, s2.c_str()) < 0; }

bool cs_mystring::operator <= (const char* s1, const cs_mystring::myString& s2) { return strcmp(s1, s2.c_str()) <= 0; }

bool cs_mystring::operator > (const char* s1, const cs_mystring::myString& s2) { return strcmp(s1, s2.c_str()) > 0; }

bool cs_mystring::operator >= (const char* s1, const cs_mystring::myString& s2) { return strcmp(s1, s2.c_str()) >= 0; }

bool cs_mystring::operator == (const char* s1, const cs_mystring::myString& s2) { return strcmp(s1, s2.c_str()) == 0; }

bool cs_mystring::operator != (const char* s1, const cs_mystring::myString& s2) { return strcmp(s1, s2.c_str()) != 0; }

std::ostream& cs_mystring::operator << (std::ostream& strm, const cs_mystring::myString& str)

{

return strm.write (str.c_str () str.length ());

}

ifstream& cs_mystring::operator >> (std::ifstream& in, myString& str) {

str.read(in, ' ');

return in;

}

void myString::read(std::ifstream& in, char ch) {

char readStr[127];

while (ch != ' ' && (in.peek() == ' ' || in.peek() == ' ')) {

in.ignore(1, ' ');

}

in.getline(readStr, 127, ch);

if (m_str != NULL) {

delete[]m_str;

}

int len = strlen(readStr);

if (readStr[len - 1] == ' ') {

readStr[len - 1] = 0;

}

m_str = new char[len+1];

strcpy(m_str, readStr);

}

myString myString::operator+(const myString &ref)const

{

int size = this->length() + ref.length();

char* str = new char[size + 1];

strcpy(str, c_str());

strcat(str, ref.c_str());

return myString(str);

}

myString myString::operator+(const char *s)const

{

int size = this->length() + strlen(s);

char* str = new char[size + 1];

strcpy(str, c_str());

strcat(str, s);

return myString(str);

}

char* cs_mystring::operator+(const char* s, const myString &ref)

{

int size = strlen(s) + ref.length();

char* str = new char[size + 1];

strcpy(str, s);

strcat(str, ref.c_str());

return str;

}

myString& myString::operator+=(const myString &ref)

{

int size = length() + ref.length();

char* str = new char[size + 1];

strcpy(str, m_str);

strcat(str, ref.c_str());

delete[] m_str;

m_str = new char[size + 1];

strcpy(m_str, str);

delete[] str;

return *this;

}

myString& myString::operator+=(const char *ref)

{

int size = length() + strlen(ref);

char* str = new char[size + 1];

strcpy(str, m_str);

strcat(str, ref);

delete[] m_str;

m_str = new char[size + 1];

strcpy(m_str, str);

delete[] str;

return *this; }

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