Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ problem. I have this String class that I am supposed to create, were not all

ID: 3749558 • Letter: C

Question

C++ problem. I have this String class that I am supposed to create, were not allowed to use the string or cstring classes or headers. I have most of it done but i am stuck with creating this, void append(const String &); // insert characters at the end of the String but before the null-term, how do i it?. Our string class involves an array of chars. We can add more varibales. Below is my code.

class String {
    public:
String(); // create an empty String object
String(const char *); // create a String object from a C-style string
String (const char); // create a String object from a single char
int length() const; // return the number of characters in the string (not including null-termination)
void append(const String &); // insert characters at the end of the String but before the null-term
String reversed() const; // return a new String object with the characters in reverse order
char operator[](int) const; // access the character at a given index
const char * c_str() const; // return a C-style string with the same characters in the same order
friend std::ostream & operator<<(std::ostream &, const String &); // allow for use with cout
char* info;
int size;
};

int String:: length() const{
    int j = 0;
    while (info[j] != '') {
        ++j;
    }
    size = j;
    return size;
}

friend std::ostream & operator<<(std::ostream out&, const String &S){
    if (S.length() > 0)   {
        for (int i=0; i < S.length(); i++)
            os << S[i];
    } else out << "";
    return out;
}

String String:: reversed() const{
    //void backword(char word[20])
String word[20];
int i=0,j=0;
char temp;
j=size(word)-1;
while(i<j) {
      temp=word[i];
      word[i]=word[j];
      word[j]=temp;
      i++;
      j--;
    }
return word;
}

String:: String(){
    length = 0;
   info   = new char[0];
    }
       
String:: String(const char C){
    length = 1;
    info = new char(C);
    }
   
String:: String(const char *C){
    if (C) {
        int m = 0;
        while (C[m] != '') m++;
        length = m;
        info = new char[C];
        for (int i=0; i < m; i++)
            info[i] = C[i];
    } else {
size = 0;
info = new char[0];
}
}
   
char String::operator[] (int i) const {
    if (i >= size) throw 1;
    return info[i];
}

Explanation / Answer

Below is the C++ code I hope that i have provided sufficient comments for your better understanding Note that I have done proper indentation but this code is automatically left alligned on this interface

void String::append(const String &s)
{
int i;
char *temp=info;
if(s.size!=0) //String to be appended should not be empty
{
info=new char[size + s.size]; //Create a new character array with length as sum of both the Strings
for(i=0;i<size;i++) //1st copy the original characters
info[i]=temp[i];
for(i=size;i<size+s.size;i++) //Now copy the characters from new string
info[i]=s.info[i-size];
}
}

Hope i have answered your question satisfactorily.Leave doubts in comment section if any.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote