Question: In C++, using the requirments: #include DynamicString.h #include #include #include #include using std::cout; using std::endl; using std::out_of_range; using std::strcmp; using std::ostringstream; template void test(int testNum,

In C++, using the requirments:

In C++, using the requirments: #include "DynamicString.h" #include #include #include #include using

#include "DynamicString.h" #include #include #include #include

using std::cout; using std::endl; using std::out_of_range; using std::strcmp; using std::ostringstream;

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

if(actual == expected){ correct++; cout

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

if(actual && expected && strcmp(actual, expected) == 0){ correct++; cout

int main(){

int testNum = 1; int correct = 0;

/*Basic initialization and accessor checks*/ /*Checks len, capacity, c_str, char_at, []*/ cout

/*Empty String*/ DynamicString s1; test(testNum++, correct, s1.len(), 0); testString(testNum++, correct, s1.c_str(), "");

/*Long string*/ DynamicString s3("1234567890qwertyuiopasdfghjklzxcvbnm"); test(testNum++, correct, s3.len(), 36); test(testNum++, correct, s3.char_at(7), '8');

/*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");

/*Really long string*/ DynamicString s5("1234567890qwertyuiopasdfghjklzxcvbnmMNBVCXZLKJHGFDSAPOIUYTREabc"); test(testNum++, correct, s5.len(), 63); test(testNum++, correct, s5.char_at(10), 'q'); test(testNum++, correct, s5[61], 'b'); testString(testNum++, correct, s5.c_str(), "1234567890qwertyuiopasdfghjklzxcvbnmMNBVCXZLKJHGFDSAPOIUYTREabc");

cout

cout

/*Suffix*/ cout

/*Comparisions*/ cout 0, true); test(testNum++, correct, s2.compare("bc")0, true); test(testNum++, correct, s4.compare("ABC ;2420* Rocks!ABC")>0, true);

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

cout 0, true); test(testNum++, correct, s2.iCompare("Bc")

test(testNum++, correct, s1.iCompare(s2)

cout

cout

cout

cout

s2.toUpper(); test(testNum++, correct, s2.len(), 3); testString(testNum++, correct, s2.c_str(), "ABC");

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

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

/*ToLower*/ cout

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

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

cout

/* Assignment */ cout

if(correct != testNum-1){ cout

/* Optional tests */ /*Bounds checking*/ cout

cout

cout

}

Requirements: Implement the following string methods O . DynamicString() //Constructs an empty string. Allocating enough memory to store the null character DynamicString(const char* str) //Constructs a string by copying the characters from the char array str to this object. You should dynamically allocate enough memory for the entire string. int len() const //returns the length of this string (i.e. the number of characters in the array not including the null char) const char* c_str() const // returns a pointer to the underlying char array char char_at(int position) const // returns the char at the specified position char& operator[](int position) // returns the char at the specified position bool startsWith(const DynamicString& other) const //True if other is a prefix of this string bool endsWith(const DynamicString& other) const //True if other is a suffix of this string int compare(const Dynamic String& other) const // negative if this is smaller than other, o if this is equal to other, positive if this is larger than other int iCompare(const DynamicString& other) const // same as compare but is case-insensetive Dynamic String& tolower() // converts the string to lowercase Dynamic String& toUpper() // converts the string to uppercase Dynamic String& replace(char old, char new) //replace all instances of old with new int find(char needle, int start=0) const //return the first index of the specified char or -1 if the char is not found starting from index start. int reverseFind(char needle, int start) const //return the right-most index (less than or equal to start) of the specified char or -1 if the char is not found. . O

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!