//This is the HEADER FILE mystring.h. //This is the INTERFACE for the class String. Values of this type are strings, //but these strings are safer than the simple strings provided automatically. #ifndef MYSTRING_H #define MYSTRING_H #include const int MAX_STRING_LENGTH = 100; class String { public: friend String operator +(const String& piece1, const String& piece2); //Returns the concatenation of piece1 and piece2; i.e., //returns the string you obtain by //writing piece2 after piece1. If the string returned is longer than //MAX_STRING_LENGTH, then any characters beyond the first //MAX_STRING_LENGTH are discarded. friend void copy_piece(String& target, const String& source, int number); //Copies the first number characters of source to target so that //those characters become the value of target. If number > source.length( ), //then only source.length( ) characters (which is all of source) //are copied to target. //Precondition: number >= 0. friend int operator ==(const String& first, const String& second); //Returns true if first and second are the same string. String( ); //Initializes the object to the empty string. String(const char a[]); //Precondition: The array a contains a string terminated with '\0'. //Action: Initializes the object so its value is the string in the array a. //If strlen(a) > MAX_STRING_SIZE, then only the first MAX_STRING_SIZE //characters in a are used for the value of the object. int length( ) const; //Returns the length of the calling object. char one_char(int position) const; //Precondition: 0 <= n < length of the calling object. //Returns the character in the nth position. //(The first character is in position 0.) void set_char(int position, char new_char); //Precondition: 0 <= n <= length of the calling object. //Action: Changes the character in the nth position to new_char. (The first //character is in position 0.) Note that you can set one position beyond //the end of the string and so add one character to the end of the string. friend istream& operator >>(istream& ins, String& the_string); //Overloads the >> operator so it can be used to input words //Precondition: If ins is a file input stream, then ins has already //been connected to a file. A string of nonwhitespace characters //followed by a whitespace character are in the input stream ins. //Action: Initial whitespace characters are read and discarded until the //first nonwhitespace character is reached. Starting with this first //nonwhitespace character, characters are read up to but not including //the next whitespace character that is encountered. The value of the //calling object is set equal to the "word" formed by the nonwhitespace //characters read. If that "word" is longer than MAX_STRING_LENGTH, //then only the first MAX_STRING_LENGTH characters are use to set the //value of the calling object, but the remaining characters up to and //including the next whitespace character encountered are read and //discarded so that they are no longer in the stream ins. friend ostream& operator <<(ostream& outs, const String& the_string); //Overloads the << operator so it can be used to output values //of type String //Precondition: If outs is a file output stream, then outs has //already been connected to a file. void input_line(istream& ins); //Precondition: If ins is a file input stream, then ins has //already been connected to a file. A string of characters followed by //the new-line character '\n' are next in the input stream ins. //Postcondition: The value of the calling object has been set to the //string read up to but not including the new-line character '\n'. //The new line character '\n' is read and discarded (but not made part //of the string value). If that string is longer than //MAX_STRING_LENGTH, then only MAX_STRING_LENGTH characters are used; //the remaining characters up to and including the new-line //character '\n' are read and discarded. private: char character[MAX_STRING_LENGTH]; int current_length; }; #endif //MYSTRING_H