Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help implementing this C++ code (Window and Linux -- Vim). when accessing character arrays in your DynamicString class, you cannot use []. You

I need help implementing this C++ code (Window and Linux -- Vim). when accessing character arrays in your DynamicString class, you cannot use []. You must use pointers.

The only place that should use [] in your DynamicString files are when you are allocating or deallocating the character arrays.

You can't use string or cstring header files. The DynamicStringTest cases must all pass.

CS2420String.h --- may be modified

--------------------------------------------------------------------------------

#ifndef CS2420_STRING_H

#define CS2420_STRING_H

/**

* Abstract CS2420String class interface.

* This class should be subclassed to provide an actual

* string class implementation. We will be creating several

* different implementations throughout the semester.

*

* You are not allowed to remove or rename any of the

* data memebers or methods from this class. However

* you can add additional data members and methods as

* you see fit. You can also change the method

* implementations if you need to.

*

*/

class CS2420String {

public:

/**

* Default constructor. Initializes an empty string.

*/

CS2420String() {}

/**

* Destructor. Declared as virtual so that subclasses

* can manage their own memory

*/

virtual ~CS2420String() {}

/** Get the number of chars in the string */

virtual int len() const = 0;

/** Get a pointer to the char buffer */

virtual const char* c_str() const = 0;

/** Get a reference to the character at the specified position.

* @param position the 0-based index to retreive.

* @return the character at the specified position.

*/

virtual char& char_at(int position) = 0;

/** Get a reference to the character at the specified position.

* @param position the 0-based index to retreive.

* @return the character at the specified position.

*/

virtual char char_at(int position) const = 0;

/** Get a reference to the character at the specified position.

* @param position the 0-based index to retreive.

* @return the character at the specified position.

*/

virtual char& operator[](int position) = 0;

/** Get a reference to the character at the specified position.

* @param position the 0-based index to retreive.

* @return the character at the specified position.

*/

virtual char operator[](int position) const = 0;

/** Returns true if other is a prefix of this string.

* @param other the string to check as a prefix.

* @return bool true if other is a prefix.

*/

virtual bool isPrefix(const CS2420String& other) const = 0;

/** Returns true if other is a prefix of this string

* ignoring character case.

* @param other the string to check as a prefix.

* @return bool true if other is a prefix.

*/

virtual bool isIPrefix(const CS2420String& other) const = 0;

/** Returns true if other is a suffix of this string.

* @param other the string to check as a suffix.

* @return bool true if other is a suffix.

*/

virtual bool isSuffix(const CS2420String& other) const = 0;

/** Returns true if other is a suffix of this string

* ignoring character case.

* @param other the string to check as a suffix.

* @return bool true if other is a suffix.

*/

virtual bool isISuffix(const CS2420String& other) const = 0;

/** Removes leading and trailing whitespace.

* The original CS2420String is not modified.

* @return a CS2420String with all leading and

* trailing whitespace characters removed.

*/

virtual CS2420String& trim() = 0;

/** Converts all characters to lowercase.

* Contents are modified "in-place"

* @return self (the lowercased string)

*/

virtual CS2420String& toLower() = 0;

/** Converts all characters to uppercase.

* Contents are modified "in-place"

* @return self (the uppercased string)

*/

virtual CS2420String& toUpper() = 0;

static const int CAPACITY = 63;

/** A pointer to the underlying null terminated char array */

char cstr[CAPACITY + 1]; //Include a spot for the null character

};

#endif

-------------------------------------------------------------------------------

DynamicString.h --- may be modified

---------------------------------------------------

#ifndef DYNAMIC_STRING_H

#define DYNAMIC_STRING_H

#include "CS2420String.h"

/**

* DynamicString class which stores strings

* using dynamically re-sized char arrays.

*

* The char array should be null-terminated so

* as to be compatible with c_string functions.

*

* Certain methods assume an 8-bit ascii char representation.

*

* You are not allowed to remove or rename any of the

* data memebers of methods from this class. However

* you can add additional data members and methods as

* you see fit. You can also change the method

* implementations as needed.

*

*/

class DynamicString : public CS2420String{

public:

/**

* Default constructor. Initializes an empty string.

*/

DynamicString();

/**

* c-string constructor. Creates a copy of

* the provided c-string.

* @param str the c-string to store.

*/

DynamicString(const char* str);

/**

* Copy-constructor. Performs a deep-copy.

* @param other the DynamicString to copy

*/

DynamicString(const DynamicString& other);

/**

* Destructor. Releases memory resources"

*/

~DynamicString();

/**

* Assignment operator. Performs a deep-copy.

* @param other the DynamicString to copy

* @return self which now contains a deep-copy of other

*/

DynamicString& operator=(const DynamicString& other);

/** Get the number of chars in the string */

int len() const;

/** Get the total number of chars that can be stored */

int capacity() const;

/** Get a pointer to the char buffer */

const char* c_str() const;

/** Get a reference to the character at the specified position.

* @param position the 0-based index to retreive.

* @return the character at the specified position.

*/

char& char_at(int position);

/** Get a copy of the character at the specified position.

* @param position the 0-based index to retreive.

* @return the character at the specified position.

*/

char char_at(int position) const;

/** Get a reference to the character at the specified position.

* @param position the 0-based index to retreive.

* @return the character at the specified position.

*/

char& operator[](int position);

/** Get a copy of the character at the specified position.

* @param position the 0-based index to retreive.

* @return the character at the specified position.

*/

char operator[](int position) const;

/** Returns true if other is a prefix of this string.

* @param other the string to check as a prefix.

* @return bool true if other is a prefix.

*/

bool isPrefix(const CS2420String& other) const;

/** Returns true if other is a prefix of this string

* ignoring character case.

* @param other the string to check as a prefix.

* @return bool true if other is a prefix.

*/

bool isIPrefix(const CS2420String& other) const;

/** Returns true if other is a suffix of this string.

* @param other the string to check as a suffix.

* @return bool true if other is a suffix.

*/

bool isSuffix(const CS2420String& other) const;

/** Returns true if other is a suffix of this string

* ignoring character case.

* @param other the string to check as a suffix.

* @return bool true if other is a suffix.

*/

bool isISuffix(const CS2420String& other) const;

/** Removes leading and trailing whitespace.

* The string is modified "in-place".

* @return a DynamicString with all leading and

* trailing whitespace characters removed.

*/

CS2420String& trim();

/** Converts all characters to lowercase.

* The string is modified "in-place".

* @return lowercase DynamicString

*/

CS2420String& toLower();

/** Converts all characters to uppercase.

* The string is modified "in-place".

* @return uppercase DynamicString

*/

CS2420String& toUpper();

/** Returns a concatenation of this string and other.

* The original strings are not modified.

* @param other the string to append.

* @returns the concatenated DynamicString

* @throws out_of_range error if the string does not fit in the buffer.

*/

DynamicString operator+(const DynamicString& other) const;

/** Returns a concatenation of this string and other.

* The original strings are not modified.

* @param other the string to append.

* @returns the concatenated DynamicString

* @throws out_of_range error if the string does not fit in the buffer.

*/

DynamicString concat(const DynamicString& other) const;

private:

/** A pointer to the underlying null terminated char array */

char* cstr;

};

#endif

-----------------------------------------------------------------

CS2420String.cpp --- may be modified

----------------------------------------------------------

#include "CS2420String.h"

#include

#include

#include

using std::out_of_range;

using std::cout;

using std::endl;

int CS2420String::len() const {

int length = 0;

while (cstr[length] != '\0')

{

length++;

}

return length;

}

const char* CS2420String::c_str() const {

return cstr;

}

char& CS2420String::char_at(int position) {

int length = len();

//check range

if (position < 0 || position >= length)

throw std::out_of_range("String out of range");

// Get a copy of the character at the specified position

return cstr[position];

}

char CS2420String::char_at(int position) const {

int length = len();

//check range

if (position < 0 || position >= length)

throw std::out_of_range("String out of range");

//Get a copy of the character at the specified position

return cstr[position];

}

char& CS2420String::operator[](int position) {

int length = len();

//check range

if (position < 0 || position >= length)

throw std::out_of_range("String out of range");

// Get a copy of the character at the specified position

return cstr[position];

}

char CS2420String::operator[](int position) const {

int length = len();

//check range

if (position < 0 || position >= length)

throw std::out_of_range("String out of range");

//Get a copy of the character at the specified position

return cstr[position];

}

bool CS2420String::isPrefix(const CS2420String& other) const {

int length1 = len();

int length2 = other.len();

// checks other and initial lengths

if (length2 <= length1)

{

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

{

//checks chars

if (cstr[i] != other.cstr[i])

return false;

}

return true;

}

return false;

}

bool CS2420String::isIPrefix(const CS2420String& other) const {

int length1 = len();

int length2 = other.len();

char letter1;

char letter2;

// checks other and initial lengths

if (length2 <= length1)

{

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

{

letter1 = cstr[i];

letter2 = other.cstr[i];

//ignores capital letters

if (isupper(letter1))

tolower(letter1);

if (isupper(letter2))

tolower(letter2);

//checks letter

if (letter1 != letter2)

return false;

}

return true;

}

return false;

}

bool CS2420String::isSuffix(const CS2420String& other) const {

int length1 = len();

int length2 = other.len();

// checks other and initial lengths

if (length2 <= length1)

{

while (length2 >= 0)

{

//checks if suffix

if (cstr[length1] != other.cstr[length2])

return false;

length1--;

length2--;

}

return true;

}

return false;

}

bool CS2420String::isISuffix(const CS2420String& other) const {

int length1 = len();

int length2 = other.len();

char letter1;

char letter2;

if (length2 <= length1)

{

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

{

letter1 = cstr[i];

letter2 = other.cstr[i];

//ignores capital letters

if (isupper(letter1))

tolower(letter1);

if (isupper(letter2))

tolower(letter2);

//checks letter

if (letter1 != letter2)

return false;

length1--;

length2--;

}

return true;

}

return false;

}

CS2420String& CS2420String::trim() {

char space;

int length = len();

int numSpace1 = 0;

int numSpace2 = length - 1;

//checks leading space

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

{

space = cstr[i];

if (isspace(space))

numSpace1++;

else

break;

}

//checks trailing space

for (int i = length - 1; i >= 0; i--)

{

space = cstr[i];

if (isspace(space))

numSpace2--;

else

break;

}

//removes space

if (numSpace1 != 0 || numSpace2 != length - 1)

{

int j = 0;

for (int i = numSpace1; i <= numSpace2; i++, j++)

cstr[j] = cstr[i];

cstr[j] = '\0';

}

return *this;

}

CS2420String& CS2420String::toLower() {

char letter;

int length = len();

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

{

//converts to lowercase

letter = cstr[i];

if (isupper(letter))

cstr[i] = tolower(letter);

}

return *this;

}

CS2420String& CS2420String::toUpper() {

char letter;

int length = len();

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

{

//converts uppercase

letter = cstr[i];

if (islower(letter))

cstr[i] = toupper(letter);

}

return *this;

}

-------------------------------------------------------

DynamicString.cpp --- may be modified

----------------------------------------------------

#include "DynamicString.h"

#include

DynamicString::DynamicString(){

//TODO:Implement me

}

DynamicString::DynamicString(const char* str){

//TODO:Implement me

}

DynamicString::DynamicString(const DynamicString& other){

//TODO:Implement me

}

DynamicString& DynamicString::operator=(const DynamicString& other){

//TODO:Implement me

}

DynamicString::~DynamicString(){

//TODO:Implement me

}

int DynamicString::len() const{

//TODO:Implement me

return -1;

}

int DynamicString::capacity() const{

//TODO:Implement me

return -1;

}

const char* DynamicString::c_str() const{

//TODO:Implement me

return nullptr;

}

char& DynamicString::char_at(int position){

//TODO:Implement me

char* a = new char('a');

return *a;

}

char DynamicString::char_at(int position) const{

//TODO:Implement me

return 'x';

}

char& DynamicString::operator[](int position){

//TODO:Implement me

char* a = new char('a');

return *a;

}

char DynamicString::operator[](int position) const{

//TODO:Implement me

return 'x';

}

bool DynamicString::isPrefix(const CS2420String& other) const{

//TODO:Implement me

return false;

}

bool DynamicString::isIPrefix(const CS2420String& other) const{

//TODO:Implement me

return false;

}

bool DynamicString::isSuffix(const CS2420String& other) const{

//TODO:Implement me

return false;

}

bool DynamicString::isISuffix(const CS2420String& other) const{

//TODO:Implement me

return false;

}

CS2420String& DynamicString::trim(){

//TODO:Implement me

return *this;

}

CS2420String& DynamicString::toLower(){

//TODO:Implement me

return *this;

}

CS2420String& DynamicString::toUpper(){

//TODO:Implement me

return *this;

}

DynamicString DynamicString::operator+(const DynamicString& other) const{

//TODO:Implement me

DynamicString a;

return a;

}

DynamicString DynamicString::concat(const DynamicString& other) const{

//TODO:Implement me

DynamicString a;

return a;

}

-------------------------------------------------------------------------

DynamicStringTest.cpp -may not be modified

-------------------------------------------------------------------------

#include "DynamicString.h"

#include

#include

#include

using std::cout;

using std::endl;

using std::out_of_range;

using std::strcmp;

template

void test(int testNum, int& correct, T actual, T expected){

if(actual == expected){

correct++;

cout << "Passed Test " << testNum << endl;

}

else{

cout << "***Failed Test " << testNum << "***" << endl;

cout << "Actual: " << actual << " Expected: " << expected << endl;

}

}

void testString(int testNum, int& correct, const char* actual, const char* expected){

if(actual && expected && strcmp(actual, expected) == 0){

correct++;

cout << "Passed Test " << testNum << endl;

}

else{

cout << "***Failed Test " << testNum << "***" << endl;

if(actual && expected){

cout << "Actual: " << actual << " Expected: " << expected << endl;

}

}

}

int main(){

int testNum = 1;

int correct = 0;

/*Basic initialization and accessor checks*/

/*Checks len, capacity, c_str, char_at, []*/

cout << "--------Accessor Tests--------" << endl;

/*Empty String*/

DynamicString s1;

test(testNum++, correct, s1.len(), 0);

test(testNum++, correct, s1.capacity(), 1);

testString(testNum++, correct, s1.c_str(), "");

/*Simple string*/

DynamicString s2("abc");

test(testNum++, correct, s2.len(), 3);

test(testNum++, correct, s2.capacity(), 4);

test(testNum++, correct, s2.char_at(0), 'a');

test(testNum++, correct, s2[2], 'c');

testString(testNum++, correct, s2.c_str(), "abc");

/*Long string*/

DynamicString s3("1234567890qwertyuiopasdfghjklzxcvbnm");

test(testNum++, correct, s3.len(), 36);

test(testNum++, correct, s3.char_at(3), '4');

/*Symbols*/

DynamicString s4("abc ;2420* Rocks!ABC");

test(testNum++, correct, s4.len(), 20);

test(testNum++, correct, s4.char_at(3), ' ');

testString(testNum++, correct, s4.c_str(), "abc ;2420* Rocks!ABC");

/*Max string*/

DynamicString s5("1234567890qwertyuiopasdfghjklzxcvbnmMNBVCXZLKJHGFDSAPOIUYTREabc");

test(testNum++, correct, s5.len(), 63);

test(testNum++, correct, s5.capacity(), 64);

test(testNum++, correct, s5[62], 'c');

testString(testNum++, correct, s5.c_str(), "1234567890qwertyuiopasdfghjklzxcvbnmMNBVCXZLKJHGFDSAPOIUYTREabc");

cout << "--------Comparison Tests--------" << endl;

cout << "--------Prefix--------" << endl;

/*Prefix*/

test(testNum++, correct, s1.isPrefix(s2), false);

test(testNum++, correct, s2.isPrefix(s1), true);

test(testNum++, correct, s2.isPrefix(s2), true);

test(testNum++, correct, s4.isPrefix(s2), true);

test(testNum++, correct, s2.isPrefix(s4), false);

test(testNum++, correct, s5.isPrefix(s3), true);

test(testNum++, correct, s3.isPrefix(s5), false);

test(testNum++, correct, s5.isPrefix(s5), true);

//test(testNum++, correct, s3.isPrefix("1234"), true);

/*IPrefix*/

cout << "--------IPrefix--------" << endl;

test(testNum++, correct, s1.isIPrefix(s2), false);

test(testNum++, correct, s2.isIPrefix(s1), true);

//test(testNum++, correct, s2.isIPrefix("AB"), true);

//test(testNum++, correct, s2.isIPrefix("ABC"), true);

test(testNum++, correct, s2.isIPrefix(s4), false);

//test(testNum++, correct, s4.isIPrefix("ABc ;2"), true);

//test(testNum++, correct, s5.isIPrefix("1234567890qweRTYuiopz"), false);

/*Suffix*/

cout << "--------Suffix--------" << endl;

test(testNum++, correct, s1.isSuffix(s2), false);

test(testNum++, correct, s2.isSuffix(s1), true);

test(testNum++, correct, s2.isSuffix(s2), true);

test(testNum++, correct, s5.isSuffix(s2), true);

test(testNum++, correct, s2.isSuffix(s5), false);

test(testNum++, correct, s5.isSuffix(s5), true);

//test(testNum++, correct, s2.isSuffix("dbc"), false);

//test(testNum++, correct, s3.isSuffix("zxcvbnm"), true);

//test(testNum++, correct, s4.isSuffix("\t"), false);

/*ISuffix*/

cout << "--------ISuffix--------" << endl;

test(testNum++, correct, s1.isISuffix(s2), false);

test(testNum++, correct, s2.isISuffix(s1), true);

//test(testNum++, correct, s2.isISuffix("aBC"), true);

//test(testNum++, correct, s4.isISuffix("s!aBc"), true);

//test(testNum++, correct, s2.isISuffix("s?AbC"), false);

cout << "--------Modification Tests--------" << endl;

cout << "--------Concatenation--------" << endl;

/*Concatenation*/

DynamicString s6 = s1+s2;

test(testNum++, correct, s6.len(), 3);

testString(testNum++, correct, s6.c_str(), "abc");

DynamicString s7 = s2+"abc";

test(testNum++, correct, s7.len(), 6);

testString(testNum++, correct, s7.c_str(), "abcabc");

DynamicString s8 = s5+s1;

test(testNum++, correct, s8.len(), 63);

testString(testNum++, correct, s8.c_str(), "1234567890qwertyuiopasdfghjklzxcvbnmMNBVCXZLKJHGFDSAPOIUYTREabc");

DynamicString s9 = s3.concat(s4);

test(testNum++, correct, s9.len(), 56);

testString(testNum++, correct, s9.c_str(), "1234567890qwertyuiopasdfghjklzxcvbnmabc ;2420* Rocks!ABC");

DynamicString s10 = s2.concat("def");

test(testNum++, correct, s10.len(), 6);

testString(testNum++, correct, s10.c_str(), "abcdef");

cout << "--------ToUpper--------" << endl;

/*ToUpper*/

DynamicString s11 = s1;

s11.toUpper();

test(testNum++, correct, s11.len(), 0);

testString(testNum++, correct, s11.c_str(), "");

DynamicString s12 = s2;

s12.toUpper();

test(testNum++, correct, s12.len(), 3);

testString(testNum++, correct, s12.c_str(), "ABC");

DynamicString s13 = s4;

s13.toUpper();

testString(testNum++, correct, s13.c_str(), "ABC ;2420* ROCKS!ABC");

DynamicString s14 = s5;

s14.toUpper();

testString(testNum++, correct, s14.c_str(), "1234567890QWERTYUIOPASDFGHJKLZXCVBNMMNBVCXZLKJHGFDSAPOIUYTREABC");

/*ToLower*/

cout << "--------ToLower--------" << endl;

DynamicString s15 = s1;

s15.toLower();

test(testNum++, correct, s15.len(), 0);

testString(testNum++, correct, s15.c_str(), "");

DynamicString s16 = s4;

s16.toLower();

testString(testNum++, correct, s16.c_str(), "abc ;2420* rocks!abc");

DynamicString s17 = s5;

s17.toLower();

testString(testNum++, correct, s17.c_str(), "1234567890qwertyuiopasdfghjklzxcvbnmmnbvcxzlkjhgfdsapoiuytreabc");

/*Trim*/

DynamicString s19 = "";

s19.trim();

test(testNum++, correct, s19.len(), 0);

testString(testNum++, correct, s19.c_str(), "");

DynamicString s20 = " abc ";

s20.trim();

test(testNum++, correct, s20.len(), 3);

testString(testNum++, correct, s20.c_str(), "abc");

DynamicString s21 = " \t!a b $ ";

s21.trim();

test(testNum++, correct, s21.len(), 6);

testString(testNum++, correct, s21.c_str(), "!a b $");

DynamicString s23 = " ";

s23.trim();

test(testNum++, correct, s23.len(), 0);

testString(testNum++, correct, s23.c_str(), "");

/*Assignment*/

s2[0] = 'b';

s3[3] = 'a';

s4[1] = 'q';

s5.char_at(8) = 'z';

s6.char_at(0) = ' ';

test(testNum++, correct, s2[0], 'b');

test(testNum++, correct, s3.char_at(3), 'a');

test(testNum++, correct, s4.char_at(1), 'q');

test(testNum++, correct, s5[8], 'z');

test(testNum++, correct, s6[0], ' ');

/*Bounds checking*/

try{

s1.char_at(1);//out_of_bounds

test(testNum++, correct, 0, 1);

}

catch(out_of_range){

test(testNum++, correct, 0, 0);

}

try{

s2.char_at(-1);//out_of_bounds

test(testNum++, correct, 0, 1);

}

catch(out_of_range){

test(testNum++, correct, 0, 0);

}

try{

s1[0];//out_of_bounds

test(testNum++, correct, 0, 1);

}

catch(out_of_range){

test(testNum++, correct, 0, 0);

}

DynamicString s25(s1);

test(testNum++, correct, s25.len(), 0);

testString(testNum++, correct, s25.c_str(), "");

DynamicString s26(s2);

testString(testNum++, correct, s26.c_str(), "bbc");

DynamicString* sptr1 = new DynamicString("Deep Copy");

DynamicString* sptr2 = new DynamicString(*sptr1);

(*sptr2)[0] = 'L';

test(testNum++, correct, (*sptr1)[0], 'D');

delete sptr2;

sptr2 = new DynamicString("Freed?");

*sptr2 = s3;

s3[1] = 'q';

test(testNum++, correct, (*sptr2)[1], '2');

delete sptr2;

delete sptr1;

sptr2 = sptr1 = nullptr;

cout << "Passed " << correct << "/" << --testNum << " tests" << endl;

cout << "Score: " << correct/float(testNum) << endl;

cout << "Points: " << 60*correct/float(testNum) << endl;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Filing And Computer Database Projects

Authors: Jeffrey Stewart

2nd Edition

007822781X, 9780078227813

More Books

Students also viewed these Databases questions

Question

How many Tables Will Base HCMSs typically have? Why?

Answered: 1 week ago

Question

What is the process of normalization?

Answered: 1 week ago