// There is the source code file for the MyString class #include "String.h" #include MyString::MyString (int len) : str_ ( new char [len + 1] ), length_ (len) { //str_ = new char [len + 1]; //length_ = len; } MyString::MyString (const char* argString) : length_ ( strlen (argString) ) { //length_ = strlen (argString); str_ = new char [length_ + 1]; strcpy (str_, argString); } // The copy constructor for the class MyString::MyString (const MyString& argString) : str_ (new char [argString.length_ + 1] ), length_ ( argString.length_ ) { //length_ = argString.length_; //str_ = new char [length_ + 1]; strcpy (str_, argString.str_); } // The destructor for the class MyString::~MyString() { cout << "In destructor for MyString: " << str_ << endl; delete [] str_; } // Returns the position of the parameter ch from the string. If the // parameter ch does not exists, return -1. The second parameter pos // is an optional parameter that specifies the postion in the string // to begin the search for the character ch. If the parameter is not // given, start at the beginning of the string (position 0) int MyString::indexOf (char ch, int pos) { while (pos < length_) { if (str_[pos] == ch) return pos; pos++; } return -1; } // This method will return true if the string given as the parameter // is wholely contained inside of the current string. bool MyString::isSubstring (const MyString& argString) { if (length_ < argString.length_) return false; for (int i = 0; i < length_; i++) { int j = 0; int tempi = i; while ((j < argString.length_) && (str_[tempi] == argString.str_[j])) { tempi++; j++; } if (j == argString.length_) return true; } return false; } // This method with join two string together into a third string MyString MyString::concat(const MyString& argString) { MyString retString (length_ + argString.length_); strcpy (retString.str_, str_); strcat (retString.str_, argString.str_); return retString; } MyString& MyString::operator=(const MyString& rhs) { if (this == &rhs) return *this; length_ = rhs.length_; delete [] str_; str_ = new char [ length_ + 1 ]; strcpy (str_, rhs.str_); return *this; } bool operator== (const MyString& lhs, const MyString& rhs) { if (lhs.length_ != rhs.length_) return false; if (strcmp(lhs.str_, rhs.str_) == 0) return true; else return false; } ostream& operator<< (ostream& ostr, const MyString& rhs) { ostr << rhs.str_; } // This code will extract a string enclosed in double quotes // set failbit in the input format is invalid istream& operator>> (istream& istr, MyString& rhs) { // check of istr is already bad //if ( !istr) //return istr; //char ch; // check for the beginning double quote character //if (ch != '"') //{ //istr.putback (ch); // replace ch back into input stream //istr.clear (ios::failbit | ios::rdstate()); //return istr; //} // read characters until the next double quote // store read characters in the "String Stream" buffer //ostrstream buf; //while (istr.get(ch) && (ch != '"')) //buf.put (ch); // encountered EOF or some other problem //if (!istr) //return istr; //buf << ends; // add end-of-string character //char *inStr = buf.str(); // using ostrstream::str() //rhs = inStr; // using MyString (const char *) and operator=() //delete [] inStr; // required by strstream //return istr; } // Example of overloading a unary operator // // Note that this operation doesn't make much sense on a // string but it shows overloading of a unary operator // // overload ! to reverse a string MyString MyString::operator! () const { MyString retVal (length_); for (int i = 0; i < length_ ; i++) retVal.str_[i] = str_[length_ - i - 1]; retVal.str_[length_] = '\0'; return retVal; } //Example of overload the [] operator char& MyString::operator[](int subscript) { if (subscript < 0 || subscript >= length_) { exit(1); } return str_[subscript]; } //Example of overload the [] operator char MyString::operator[](int subscript) const { if (subscript < 0 || subscript >= length_) { exit(1); } return str_[subscript]; }