Question: Complete the code: From(DynamicString& DynamicString::replace(char old, char newCh){) #include DynamicString.h #include #include using std::out_of_range; using std::tolower; using std::toupper; using std::ostream; using namespace std; DynamicString::DynamicString(){ cstr

Complete the code: From(DynamicString& DynamicString::replace(char old, char newCh){)

Complete the code: From(DynamicString& DynamicString::replace(char old, char newCh){) #include "DynamicString.h" #include #include

#include "DynamicString.h" #include #include

using std::out_of_range; using std::tolower; using std::toupper; using std::ostream; using namespace std;

DynamicString::DynamicString(){ cstr = new char[1]; cstr[0] = '\0'; }

DynamicString::DynamicString(const char* str){ int count = 0; while (str[count]) { ++count; } this->cstr = new char[count + 1]; for (int i = 0; i cstr[i] = str[i]; } this->cstr[count] = '\0'; }

int DynamicString::len() const{ int count = 0; while (cstr[count]) { ++count; } return count; }

const char* DynamicString::c_str() const{ return cstr;

}

char DynamicString::char_at(int position) const{ return cstr[position]; }

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

return cstr[index]; }

bool DynamicString::startsWith(const DynamicString& other) const{ if (other.len()

bool DynamicString::endsWith(const DynamicString& other) const{ int len1 = len(); int len2 = other.len(); if (len2

int DynamicString::compare(const DynamicString& other) const{ // Get size of this string int size1 = this->len(); // Get size of other string int size2 = other.len();

int i = 1; // Iterate through the characters while ((i != size1) || (i != size2)) { // If this is less than other if (cstr[i]

// If this is greater than other } else if (cstr[i] > other.cstr[i]) { return 1; } i++; }

// If this is equal to other return 0; }

int DynamicString::iCompare(const DynamicString& other) const { // Get size of this string int size1 = this->len(); // Get size of other string int size2 = other.len();

int i = 1; // Iterate through the characters while ((i != size1) || (i != size2)) { // Compare each character after converting to lower case // to make it case insensitive comparison. Or, we can also // convert it to upper case instead of lower case.

// If this is less than other if (tolower(cstr[i])

// If this is greater than other } else if (tolower(cstr[i]) > tolower(other.cstr[i])) { return 1; } i++; }

// If this is equal to other return 0; }

DynamicString& DynamicString::toLower() { // Iterate through the characters of this string for (int i = 0; i

DynamicString& DynamicString::toUpper() { // Iterate through the characters of this string for (int i = 0; i

int DynamicString::find(char c, int start) const{ return -1; }

int DynamicString::reverseFind(char c, int start) const{ return -1; }

int DynamicString::reverseFind(char c) const{ return -1; }

ostream& operator 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!