Answer to Question #331433 in C++ for Junior JIA TSAF

Question #331433

Your goal is to implement a generic “String” class using char array. Please also write down the test code to drive all functions of your class implementation

class String{

private: //think about private members

public: //give definitions of following functions

String(); -default constructor

String(char *str); -initializes string with constant cstring

String(const String &); -copy constructor

String(int x); -initialize string of pre-defined size

void setAt(int i, char c); -set character at index [x]

String substr(int pos, int len); -return substring of length len from ‘pos’

String substr(int pos); -return substring from given position to end

\void append(String str ); -append a String at end of string

void append(char *str ); -append a constant c string at end of string

char * tocstring(); -convert String to c-string

void display(); -display string

bool isEmpty(); -return true if string is empty

void copy(const String&); -copy one string to another

void copy(const char *); -copy cstring to String

};


1
Expert's answer
2022-04-21T07:02:54-0400
#include <iostream>


class String {
  private:
    int length;
    char* data;
  public:
    String() : length(0), data(nullptr) {}
    String(const char* str) : length(0) {
      while (str[length] != '\0') {
        ++length;
      }
      data = new char[length];
      for (int i = 0; i < length; ++i) {
        data[i] = str[i];
      }
    }
    String(const String& str) : length(str.length), data(new char[length]) {
      for (int i = 0; i < length; ++i) {
        data[i] = str.data[i];
      }
    }
    String(int i) : length(i), data(new char[length]) {}
    void setAt(int i, char c) {
      data[i] = c;
    }
    String substr(int pos, int len) {
      String result(len);
      for (int i = 0; i < len; ++i) {
        result.data[i] = data[pos + i];
      }
      return result;
    }
    String substr(int pos) {
      int len = length - pos;
      String result(len);
      for (int i = 0; i < len; ++i) {
        result.data[i] = data[pos + i];
      }
      return result;
    }
    void append(String str) {
      char* temp = data;
      data = new char[length + str.length];
      for (int i = 0; i < length; ++i) {
        data[i] = temp[i];
      }
      delete[] temp;
      for (int i = 0; i < str.length; ++i) {
        data[length + i] = str.data[i];
      }
      length += str.length;
    }
    void append(const char* str) {
      int len = 0;
      while (str[len] != '\0') {
        ++len;
      }
      char* temp = data;
      data = new char[length + len];
      for (int i = 0; i < length; ++i) {
        data[i] = temp[i];
      }
      delete[] temp;
      for (int i = 0; i < len; ++i) {
        data[length + i] = str[i];
      }
      length += len;
    }
    char* tocstring() {
      char* result = new char[length + 1];
      for (int i = 0; i < length; ++i) {
        result[i] = data[i];
      }
      result[length] = '\0';
      return result;
    }
    void display() {
      for (int i = 0; i < length; ++i) {
        std::cout << data[i];
      }
    }
    bool isEmpty() {
      return length == 0;
    }
    void copy(const String& str) {
      if (length != str.length) {
        length = str.length;
        if (data != nullptr) {
          delete[] data;
        }
        data = new char[length];
      }
      for (int i = 0; i < length; ++i) {
        data[i] = str.data[i];
      }
    }
    void copy(const char* str) {
      int len = 0;
      while (str[len] != '\0') {
        ++len;
      }
      if (length != len) {
        length = len;
        if (data != nullptr) {
          delete[] data;
        }
        data = new char[length];
      }
      for (int i = 0; i < length; ++i) {
        data[i] = str[i];
      }
    }
    ~String() {
      if (data != nullptr) {
        delete[] data;
      }
    }
};


int main() {
  String a;
  std::cout << "Empty string A:\n";
  a.display();
  String b("The quick brown fox jumps over the lazy dog");
  std::cout << "\nString B:\n";
  b.display();
  String c(b);
  std::cout << "\nCopy of B:\n";
  c.display();
  String d(10);
  std::cout << "\nCreated string of size 10 (does not contain letters):\n";
  d.display();
  b.setAt(16, 'b');
  std::cout << "\nString B with changed letter (should be 'box' instead of 'fox'):\n";
  b.display();
  std::cout << "\nb.substr(16, 9):\n";
  b.substr(16, 9).display();
  std::cout << "\nb.substr(16):\n";
  b.substr(16).display();
  b.append(String(", and runs"));
  b.append(" away");
  std::cout << "\nString B after appending other strings:\n";
  b.display();
  char* c_str = b.tocstring();
  std::cout << "\nC-string:\n" << c_str;
  a.copy(b);
  delete c_str;
  std::cout << "\nCopied B into A:\n";
  a.display();
  a.copy("The end");
  std::cout << "\nCopied 'The end' into A:\n";
  a.display();
  std::cout << '\n';
  return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS