// There is the source code file for the String class #include "String.h" #include String::String (int len) { str_ = new char [len + 1]; length_ = len; } String::String (const char* argString) { length_ = strlen (argString); str_ = new char [length_ + 1]; strcpy (str_, argString); } // The copy constructor for the class String::String (const String& argString) { length_ = argString.length_; str_ = new char [length_ + 1]; strcpy (str_, argString.str_); } // The destructor for the class String::~String() { 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 String::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 String::isSubstring (const String& 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 String String::concat(const String& argString) { String retString (length_ + argString.length_); strcpy (retString.str_, str_); strcpy (retString.str_, argString.str_); return retString; } String& String::operator=(const String& 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 String& lhs, const String& 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 String& 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, String& 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 String (const char *) and operator=() deleter [] 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 String String::operator! () { String retVal (length_); for (int i = 0; i < length_ ; i++) retVal.str_[i] = str_[length_ - i - 1]; retVal.str[length_] = '\0'; return retVal; }