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.

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, const char * c_str() const; // return a C-style string with the same characters in the same order, how do i it?. Our string class involves an array of chars. We can add more variables. 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] != '\0') { ++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= size) throw 1; return info[i]; }

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!