// String.h // ComS 228 Fall 2005 // Homework 2 // #include #include // string library for C-style strings #include // library for the isspace() function using namespace std; #ifndef ALT_STRING_CLASS #define ALT_STRING_CLASS class String { public: // CONSTRUCTORS and DESTRUCTOR String(); // post: creates an empty String String(char c); // pre: c is any valid character // post: creates a String of length one that contains c String(char* s); // pre: s is a null-terminated array of characters, // post: this String contains the sequence of characters from s String(const String& str); // post: this String is a deep copy of str ~String(); // post: the memory occupied by the String has been deallocated // MODIFICATION MEMBER FUNCTIONS String& operator = (const String& str); //post: if this String and str are not the same object, then this // String is a deep copy of str. the return value is // this String void operator += (const String& add); //pre: this String and add are different objects //post: add has been concatenated to the end of this String void operator += (char* add); //pre: add is a null-terminated character array //post: add has been concatenated to the end of this String void operator += (char add); //post: the single character add has been concatenated to the end // of this String void reverseString(); // post: the sequence of characters (not including the null character) // in this String have been reversed. // example: if myGreeting is a String containing "Hello", then after // myGreeting.reverseString(), myGreeting contains "olleH". // CONSTANT MEMBER FUNCTIONS String operator + (const String& str) const; // post: the return value is a String which contains the sequence of // characters from this String followed by the sequence of characters // from str // example: if myGreeting is a String containing "Hello" and myFriend // is a String containing "World", then the return value of // myGreeting + myFriend is "HelloWorld". String getSubstring (int from , int to) const; // pre: from and to are between 0 and one less than the length of // String, and from <= to // post: return value is a String containing the same sequence of // of characters as this String from the "from-th" // character to the "to-th" character, inclusive. // example: if myGreeting is a String containing "Hello", then the // return value of myGreeting.getSubstring(2,4) is "llo". int isSubstring(const String& s) const; // post: If s is a substring of this String, then return // the character position in String where s starts // If s is not a substring of String, then return -1. // example: if myGreeting is a String containing "Hello", then // the return value of myGreeting.isSubstring("el") is // 1 and the return value of myGreeting.isSubstring("low") // is -1. int len() const; // post: returns the number of characters in this String, not counting // the null character char& operator [](int i) const; // pre: i is between 0 and one less than the length of this String // post: return value is the i-th character in this String // note: because this function returns by reference, it may be used // as an lvalue (i.e. on the left side of an assignment // statement). for example, myGreeting[0] = 'h'; would change // myGreeting from "Hello" to "hello". bool operator < (const String& str2) const; // post: returns true if this String < str2, lexicographically, // otherwise returns false bool operator > (const String& str2) const; // post: returns true if this String > str2, lexicographically, // otherwise returns false bool operator >= (const String& str2) const; // post: returns true if this String >= str2, lexicographically, // otherwise returns false bool operator <= (const String& str2) const; // post: returns true if this String <= str2, lexicographically, // otherwise returns false bool operator == (const String& str2) const; // post: returns true if this String equals str2, lexicographically, // otherwise returns false bool operator != (const String& str2) const; // post: returns true if this String does not equal str2, // lexicographically, otherwise returns false // FRIEND FUNCTIONS friend ostream& operator << (ostream& outs, const String& str); // post: prints the sequence of characters in this String to outs, // and the return value is outs friend istream& operator >> (istream& ins, String& str); // post: skipping leading whitespace, reads in a sequence of // non-whitespace characters into this String from ins, and the // return value is ins // note: the whitespace character that ends the input should be // left on ins private: // A String object stores a null-terminated dynamic character array and its // length (not counting the null character). There should be exactly one // null character in the array. // // An empty String object contains only the null character in theString and // is of length zero. // char *theString; int length; }; #endif