Question: see and use; // mystring.h /* Comments: String will be created as an empty string of capacity 20 initially. */ #include class mystring{ friend std::ostream&

 see and use; // mystring.h /* Comments: String will be createdas an empty string of capacity 20 initially. */ #include class mystring{

see and use;

// mystring.h /* Comments: String will be created as an empty string of capacity 20 initially. */ #include  class mystring{ friend std::ostream& operator> (std::istream &is, mystring &st); public: mystring(unsigned int capacity=20); // str is an array of characters, size its size mystring(const mystring &str); // copy constructor ~mystring(); // destructor mystring& operator+=(char c); // concatenating string with a character /* uncomment when the above is implemented and checked char& operator[](unsigned int position); int find(char c); // finds the first occurence of c in the string, returns its position, -1 if not found void reverse(); // reverses the string */ /* mystring& operator=(const mystring &str); void copy(const mystring &str); // used by copy constructor and in assignment operator mystring& operator+=(const mystring &str); */ private: void resize(unsigned int newcapacity); char *string_; unsigned int size_; unsigned int capacity_; }; std::ostream& operator>(std::istream &is, mystring &st);

---------------------------------------------------------------------------------

// mystring.cpp #include  #include  #include "mystring.h" mystring::mystring(unsigned int capacity){ size_ = 0; // empty string capacity_ = capacity; string_ = new char[capacity]; } mystring::~mystring(){ // put the code here } mystring& mystring::operator+=(char c){ // put the code here for adding one character to the string return *this; } void mystring::resize(unsigned int newcapacity){ char *tmp; // tmp will be used for the "new" placeholder of array // put the code here for resizing the string to be of new capacity 'newcapacity' // don't forget to copy over elements from the "old" place to the "new" one delete [] string_; // deallocate the space by "old" array string_ = tmp; // re-direct the pointer to the "new" array } std::ostream& operator// put the code here for outputting characters, one by one, from the string_ array to the 'os'  os // put the rest of the code here } int mystring::find(char c) { // finds the first occurence of c in the string, returns its position, // returns -1 if not found // put the code here } void mystring::reverse() { // put the code for string reversal here } */ mystring::mystring(const mystring &str) { copy(str); } void mystring::copy(const mystring &str){ // this procedure copies everything from string 'str' to "this" string // size_ has to be updated, capacity_ has to be updated // new space should be allocated for the array (use tmp) // at the end don't forget to do string_ = tmp; } mystring& mystring::operator=(const mystring &str) { delete[] string_; copy(str); return *this; } /* operator overloading */ /* comment : actually it is advised not to do operator overloading on the same number of parameters (1 parameter in our case), especially when their types are "close". For example in this case, we defined operator+= on character and on mystring types, which are pretty close. So in real world, we won't do it. Instead we will most likely define two operators: addchar addstring */ /* mystring& mystring::operator+=(const mystring &str) { // this is concatentation of two strings operator for two strings, i.e. s1 += s2 // put your code here return *this; } std::istream& operator>> (std::istream &is, mystring &st) { unsigned int length; std::cout > length; st.capacity_ = length; st.resize(length); int i = 0; char inputItem; std::cout > inputItem; st.string_[i] = inputItem; i++; } st.size_ = length; return is; } */ --------------------------------------------------------------
// string_using.cpp #include  #include "mystring.h" using namespace std; int main(){ mystring s1; s1 += 'H'; s1 += 'e'; s1 += 'l'; s1 += 'l'; s1 += 'o'; s1 += ','; s1 += ' '; s1 += 'w'; s1 += 'o'; s1 += 'r'; s1 += 'l'; s1 += 'd'; s1 += '!'; cout > s5; cout > i; }

strings, access the element at a specific position, and input/output instances of your string class. Also add some of the methods that the Python or C++ string class support such as slicing/substrings, searching for an element, reversing a string, and so on. Name your class MyString to avoid confusion with the name of the existing string class

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!