Question

//codes in DynamicString.cpp #include "DynamicString.h" int DynamicString::myStringLen(const char* str){ //TODO::Implement me return -1; } DynamicString::DynamicString(){ //TODO::1::Implement...

//codes in DynamicString.cpp

#include "DynamicString.h"

int DynamicString::myStringLen(const char* str){
//TODO::Implement me
return -1;
}

DynamicString::DynamicString(){
//TODO::1::Implement me
}

DynamicString::DynamicString(const char* str){
//TODO::1::Implement me
}

int DynamicString::len() const{
//TODO::1::Implement me
return -1;
}

const char* DynamicString::c_str() const{
//TODO::1::Implement me
return nullptr;
}

char& DynamicString::char_at(int position){
//TODO::1::Implement me
char* a = new char('a');
return *a;
}

char DynamicString::char_at(int position) const{
//TODO::1::Implement me
return 'a';
}

char& DynamicString::operator[](int position){
//TODO::1::Implement me
char* a = new char('a');
return *a;
}

char DynamicString::operator[](int position) const{
//TODO::1::Implement me
return 'a';
}

bool DynamicString::isPrefix(const DynamicString& other) const{
//TODO::1::Implement me
return false;
}

bool DynamicString::isIPrefix(const DynamicString& other) const{
//TODO::1::Implement me
return false;
}

bool DynamicString::isSuffix(const DynamicString& other) const{
//TODO::1::Implement me
return false;
}

bool DynamicString::isISuffix(const DynamicString& other) const{
//TODO::1::Implement me
return false;
}

int DynamicString::compare(const DynamicString& other) const{
//TODO::1::Implement me
return 0;
}

int DynamicString::iCompare(const DynamicString& other) const{
//TODO::1::Implement me
return 0;
}

DynamicString& DynamicString::toLower(){
//TODO::1::Implement me
return *this;
}

DynamicString& DynamicString::toUpper(){
//TODO::1::Implement me
return *this;
}


DynamicString::DynamicString(const DynamicString& other){
//TODO::2::Implement me
}

DynamicString::~DynamicString(){
//TODO::2::Implement me
}

DynamicString& DynamicString::operator=(const DynamicString& other){
//TODO::2::Implement me
return *this;
}

DynamicString DynamicString::trim(){
//TODO::2::Implement me
return *this;
}

DynamicString DynamicString::operator+(const DynamicString& other) const{
//TODO::2::Implement me
return *this;
}

DynamicString DynamicString::concat(const DynamicString& other) const{
//TODO::2::Implement me
return *this;
}

int DynamicString::find(int start, char c) const{
//TODO::2::Implement me
return -1;
}

int DynamicString::reverseFind(int start, char c) const{
//TODO::2::Implement me
return -1;
}

void DynamicString::subStr(char* buf, int start, int end) const{
//TODO::2::Implement me
}

String Library Part 1 Assignment

When implementing the String class you are not allowed to use the string header file, cstring header file or C++ strings.

Requirements:

Implement the following string methods correctly and pass all the test cases

  • DynamicString() //Constructs an empty string
  • DynamicString(const char* str) //Constructs a string by copying the characters from he char array str
  • int len() const //returns the length of this string
  • const char* c_str() const // returns a pointer to the underlying char array
  • char& char_at(int position) //returns the char at the specified position
  • char char_at(int position) const // returns the char at the specified position
  • char& operator[](int position) // returns the char at the specified position
  • char operator[](int position) const // returns the char at the specified position
  • bool isPrefix(const DynamicString& other) const //True if other is a prefix of this string
  • bool isIPrefix(const DynamicString& other) const //True if other is a prefix of this string ignoring case(capitalization)
  • bool isSuffix(const DynamicString& other) const //True if other is a suffix of this string
  • bool isISuffix(const DynamicString& other) const //True if other is a suffix of this string ignoring case (capitalization)
  • int compare(const DynamicString& other) const // negative if this is smaller than other, 0 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
  • DynamicString& toLower() // converts the string to lowercase
  • DynamicString& toUpper() // converts the string to uppercase

Helpful C++ library methods:

  • tolower
  • toupper
  • out_of_range
0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include "DynamicString.h"
#include <iostream>
#include <stdexcept>
#define DEFAULT_SIZE 20
using std::cout;
using std::endl;

int DynamicString::myStringLen(const char* str){
   
        int size = 0;
        while (*str++) size++;
        return size;
        
}

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


}

DynamicString::DynamicString(const char* str) {

        int size = myStringLen(str);
        cstr = new char[size + 1];

        for (int i = 0; i <= size; i++)
                cstr[i] = str[i];

}

int DynamicString::len() const {
        
        int len = 0;
        while (cstr[len] != '\0')
        {
                len++;
        }

        return len;
        

}

const char* DynamicString::c_str() const {

        return cstr;
}

char& DynamicString::char_at(int position) {
        if (position >= len() || position < 0)
                throw std::out_of_range("NUMBER IS OUT OF RANGE");

        return cstr[position];
}

char DynamicString::char_at(int position) const {
        if (position >= len() || position < 0)
                throw std::out_of_range("NUMBER IS OUT OF RANGE");

        return cstr[position];
}

char& DynamicString::operator[](int position) {
        if (position >= len() || position < 0)
                throw std::out_of_range("NUMBER IS OUT OF RANGE");
        
        return cstr[position];
}

char DynamicString::operator[](int position) const {
        if (position >= len() || position < 0)
                throw std::out_of_range("NUMBER IS OUT OF RANGE");

        return cstr[position];
}

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


                for (int i = 0; i < other.len(); i++)
                {
                        if (cstr[i] != other.cstr[i]) {

                                return false;
                        }
                }
                return true;

}

        


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

        char lettercstr;
        char letterother;

        if (other.len() <= len()) {

                for (int i = 0; i < other.len(); i++)
                {
                        lettercstr = cstr[i];
                        if (lettercstr >= 'A' && lettercstr <= 'Z')
                                lettercstr = lettercstr - 'A' + 'a';

                        letterother = other.cstr[i];
                        if (letterother >= 'A' && letterother <= 'Z')
                                letterother = letterother - 'A' + 'a';

                        if (lettercstr != letterother)
                                return false;

                }
                return true;
        }

        return false;

}

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

        int cstrlength = len();
        int otherlength = other.len();

        if (otherlength <= cstrlength) {

                while (otherlength >= 0) {

                        if (cstr[cstrlength] != other.cstr[otherlength]) {

                                return false;
                        }
                        cstrlength--;
                        otherlength--;  
                }
                return true;
        }
        return false;
}

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

        int cstrlength = len();
        int otherlength = other.len();

        char cstrletter;
        char otherletter;

        if (otherlength <= cstrlength) {

                while (otherlength >= 0) {

                                cstrletter = cstr[cstrlength];
                                if (cstrletter >= 'A' && cstrletter <= 'Z')
                                        cstrletter = cstrletter - 'A' + 'a';

                                otherletter = other.cstr[otherlength];
                                if (otherletter >= 'A' && otherletter <= 'Z')
                                        otherletter = otherletter - 'A' + 'a';

                                        if (cstrletter != otherletter) {

                                                return false;
                                        }
                                        cstrlength--;
                                        otherlength--;
                }
                return true;
        }
        return false;
}

int DynamicString::compare(const DynamicString& other) const {
        return 100;
}

int DynamicString::iCompare(const DynamicString& other) const {
        return 10;
}

DynamicString& DynamicString::toLower() {
        
        char cstrletter;

        for (int i = 0; i <= len(); i++) {
                cstrletter = cstr[i];

                        if (isupper(cstrletter))
                                cstr[i] = tolower(cstrletter);
        }
        return *this;
}

DynamicString& DynamicString::toUpper() {

        char cstrletter;

        for (int i = 0; i <= len(); i++) {
                cstrletter = cstr[i];

                if (islower(cstrletter))
                        cstr[i] = toupper(cstrletter);
        }
        return *this;
}


DynamicString::DynamicString(const DynamicString& other) {
        int size = other.len();
        cstr = new char[other.len() + 1];
        copyStr(cstr, other.cstr, other.len());
}

DynamicString::~DynamicString() {
        if (cstr) {
                delete cstr;
                cstr = nullptr;
        }
}

DynamicString& DynamicString::operator=(const DynamicString& other) {
        int size = other.len();
        if (cstr) {
                delete cstr;
                cstr = nullptr;
        }
        cstr = new char[other.len() + 1];
        if (this != &other) {
                for (int i = 0; i < size + 1; i++) {
                        cstr[i] = other.cstr[i];
                }
        }
        return *this;
}

DynamicString& DynamicString::trim() {
        int first = 0;
        while (cstr[first] == ' ' || cstr[first] == '\n' || cstr[first] == '\r' || cstr[first] == '\t') {

                first++;
        }

        int second = len() - 1;
        while (cstr[second] == ' ' || cstr[second] == '\n' || cstr[second] == '\r' || cstr[second] == '\t') {

                second--;
        }

        if (first != 0 || second != len() - 1) {
                int x = 0;
                int i;

                for (i = first; i <= second; i++, x++) {

                        cstr[x] = cstr[i];

                }
                cstr[x] = '\0';
        }

return *this;

}

DynamicString DynamicString::operator+(const DynamicString& other) const {
        return this->concat(other);
}

DynamicString DynamicString::concat(const DynamicString& other) const {
        char* concat = new char[len() + other.len() + 1];
        copyStr(concat, cstr, len());
        copyStr(concat + len(), other.cstr, other.len());
        DynamicString retVal(concat);
        delete[] concat;
        concat = nullptr;

        return retVal;
}


int DynamicString::find(int start, char c) const {

        for (int i = start; i < len(); i++) {

                if (cstr[i] == c) {
                        return i;
                }
                
        }
return -1;
}

int DynamicString::reverseFind(int start, char c) const {
        for (int i = start; i >= 0; i--) {
                if (cstr[i] == c) {
                        return i;
                }
        }
        return -1;
}

void DynamicString::subStr(char* buf, int start, int end) const {

                for (int i = start, j = 0; i < end; i++, j++) {
                        buf[j] = cstr[i];
                }


                buf[end - start] = '\0';
        
        
}

void DynamicString::copyStr(char* buf, char* old, int size) const {
        for (int i = 0; i <= size; i++) {
        buf[i] = old[i];

        }
        buf[size] = '\0';

}
Add a comment
Know the answer?
Add Answer to:
//codes in DynamicString.cpp #include "DynamicString.h" int DynamicString::myStringLen(const char* str){ //TODO::Implement me return -1; } DynamicString::DynamicString(){ //TODO::1::Implement...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ When running my tests for my char constructor the assertion is coming back false and...

    C++ When running my tests for my char constructor the assertion is coming back false and when printing the string garbage is printing that is different everytime but i dont know where it is wrong Requirements: You CANNOT use the C++ standard string or any other libraries for this assignment, except where specified. You must use your ADT string for the later parts of the assignment. using namespace std; is stricly forbiden. As are any global using statements. Name the...

  • c++ int count(const string& str, char a); Write a recursive function that finds the number of...

    c++ int count(const string& str, char a); Write a recursive function that finds the number of occurrences of a specified letter in a string. For example,count("Welcome",’e’)returns 2.

  • #include <stdio.h> #include <stdlib.h> #include <string.h> #include<ctype.h> #define MAX_LEN 255 int numWords(char *str); int numDigit(char *str);...

    #include <stdio.h> #include <stdlib.h> #include <string.h> #include<ctype.h> #define MAX_LEN 255 int numWords(char *str); int numDigit(char *str); int numUppltr(char *str); int numLwrltr(char *str); int punChar(char *str); char*readString(char *str); int main() { char givString[MAX_LEN]; puts("Enter your string(Max 255 characters):"); //readString(givString); gets(givString); printf("\nnumber of words :%d",numWords(givString)); printf("\nnumber of uppercase letters %d",numUppltr(givString)); printf("\nnumber of lowercase letters %d",numLwrltr(givString)); printf("\nnumber of punctuations %d\n",punChar(givString)); printf("\nnumber of digits:%d\n",numDigit(givString)); system("pause"); return 0; } char *readString(char *str) { int ch, i=0; while((ch=getchar())!=EOF && ch!='\n') { if(i) { str[i]=ch; i++; }...

  • C++ Programming I have finished the code but there's one error which shows that "strcpy" might...

    C++ Programming I have finished the code but there's one error which shows that "strcpy" might be unsafe. Consider using strcpy_s instead. I've tried that but it's not working probably because I didn't implement it correctly. I am posting my code below. It'd be really helpful if you could fix all the strcpy errors and post it below. Thank you. Text.cpp: #include <iostream> #include <iomanip> #include <cassert> #include <cstring> #include "Text.h" Text::Text ( const char *charSeq ) {    bufferSize...

  • #include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str);...

    #include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str); int numConsonants(char *str); int main() {    char string[100];    char inputChoice, choice[2];    int vowelTotal, consonantTotal;    //Input a string    cout << "Enter a string: " << endl;    cin.getline(string, 100);       do    {        //Displays the Menu        cout << "   (A) Count the number of vowels in the string"<<endl;        cout << "   (B) Count...

  • ​​​​​​public static int countCharacter(String str, char c) { // This recursive method takes a String and...

    ​​​​​​public static int countCharacter(String str, char c) { // This recursive method takes a String and a char as parameters and // returns the number of times the char appears in the String. You may // use the function charAt(int i) described below to test if a single // character of the input String matches the input char. // For example, countCharacter(“bobbie”, ‘b’) would return back 3, while // countCharacter(“xyzzy”, ‘y’) would return back 2. // Must be a RECURSIVE...

  • #include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str);...

    #include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str); int numConsonants(char *str); int main() {    char string[100];    char inputChoice, choice[2];    int vowelTotal, consonantTotal;    //Input a string    cout << "Enter a string: " << endl;    cin.getline(string, 100);       do    {        //Displays the Menu        cout << "   (A) Count the number of vowels in the string"<<endl;        cout << "   (B) Count...

  • Please write the code in a text editor and explain thank you! 1. LINKED-LIST: Implement a...

    Please write the code in a text editor and explain thank you! 1. LINKED-LIST: Implement a function that finds if a given value is present in a linked-list. The function should look for the first occurrence of the value and return a true if the value is present or false if it is not present in the list. Your code should work on a linked-list of any size including an empty list. Your solution must be RECURSIVE. Non-recursive solutions will...

  • Below is the given code of implementation: #include <string> #include <iostream> #include <list> #include <cassert> using...

    Below is the given code of implementation: #include <string> #include <iostream> #include <list> #include <cassert> using namespace std; class List; class Iterator; class Node { public: /* Constructs a node with a given data value. @param s the data to store in this node */ Node(string s); /* Destructor */ ~Node() {} private: string data; Node* previous; Node* next; friend class List; friend class Iterator; }; class List { public: /** Constructs an empty list. */ List(); /* Destructor. Deletes...

  • // Name.h #ifndef __NAME_H__ #define __NAME_H__ #include <iostream> #include <string> using namespace std; #define MAXLENGTH 12...

    // Name.h #ifndef __NAME_H__ #define __NAME_H__ #include <iostream> #include <string> using namespace std; #define MAXLENGTH 12 #define NAME_CHARS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-'" namespace Errors {     struct InvalidName { }; } class Name { public:     Name        ( string first_name = "", string last_name = "");     string first() const;     string last() const;     void   set_first( string fname );     void   set_last( string lname);     friend ostream& operator<< ( ostream & os, Name name );     friend bool operator== (Name name1, Name...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT