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

Any help for this C++ question would be absolutely positively awesome! A string

ID: 3622427 • Letter: A

Question

Any help for this C++ question would be absolutely positively awesome!

A string is a lot like a vector (or resizable array) of characters. Imagine that strings do not exist in C++, and you have to create them. You would need to:

A. Define a suitable class or struct for representing a string of characters, realising that strings can have any length whatsoever, from zero up.

B. Define a constructor that sets up an initially empty string.

C. Create a method that allows a single character to be added to the end of one of your strings.

D. Create a method that allows one string to be added to the end of another.

E. Create a function that will compare two strings to tell you whether they are the same, ignoring the difference between capital and little letters (so that “CAT” is considered to be the same as “cat”).

Do those things.

______________________________________________________________________________
As an aid to understanding, here is a possible use of your creations:

MyString one, two, three;
one.add('c');
one.add('a');
two.add('C');
two.add('A');
two.add('T');
one.add('t');
three.add(two);
three.add('-');
three.add(one);
three.add(two);
if (same(one, two))
cout << "Yes.";
if (!same(two, three))
cout << "No.";

That should cause “Yes.No.” to be printed, and at the end one would hold “cat”, two would hold “CAT”, and three would hold “CAT-catCAT”.

Explanation / Answer

Hope this helps. Let me know if you have any questions. Please rate. :) I'm not sure if you wanted this in separate files, like "MyString.h" "MyString.cpp" and "main.cpp." Here is all the code you would need. It would not be difficult to separate the specific parts into their own files. This displays what is asked - the "Yes.No." Also, it correctly stores the specified strings, "cat" "CAT" and "CAT-catCAT" but they aren't displayed. #include #include using namespace std; class MyString { public: MyString(); bool same(MyString other); void add(char c); void add(MyString str); private: char * theString; int curLen; int maxLen; }; MyString::MyString() { curLen = 0; maxLen = 0; theString = new char[1]; theString[0] = ''; } bool MyString::same(MyString other) { int len = strlen(other.theString); if (len != this->curLen) return false; for (int i = 0; i theString[i]) != tolower(other.theString[i])) return false; } return true; } void MyString::add(char c) { if (curLen + 1 > maxLen) { if (maxLen == 0) { delete [] theString; maxLen = 16; theString = new char[maxLen + 1]; } else { char * temp; maxLen *= 2; temp = new char[maxLen + 1]; strcpy(temp, theString); delete [] theString; theString = temp; } } theString[curLen] = c; theString[curLen+1] = ''; curLen++; } void MyString::add(MyString str) { int len = strlen(str.theString); for (int i = 0; i
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