//This is the IMPLEMENTATION FILE mystring.cxx //(Your system may require some suffix other than .cxx.) //this is the IMPLEMENTATION for the class String. //The interface for the class String is in the header file mystring.h. #include #include #include #include "mystring.h" const int FALSE = 0; const int TRUE = 1; String operator +(const String& piece1, const String& piece2) { String temp; temp.current_length = piece1.current_length + piece2.current_length; if (temp.current_length > MAX_STRING_LENGTH) temp.current_length = MAX_STRING_LENGTH; int i; for (i = 0; i < piece1.current_length; i++) temp.character[i] = piece1.character[i]; int offset = i; for (i = offset; i < temp.current_length; i++) temp.character[i] = piece2.character[i - offset]; return temp; } //uses stdlib.h: void copy_piece(String &target, const String& source, int number) { if (number < 0) { cout << "ERROR: illegal call to copy_piece.\n"; exit(1); } if (number > source.current_length) number = source.current_length; for (int i = 0; i < number; i++) target.character[i] = source.character[i]; target.current_length = number; } int operator ==(const String& first, const String& second) { if (first.current_length != second.current_length) { return FALSE; } else { int i = 0, mismatch = FALSE; while ((i < first.current_length) && (! mismatch)) { if (first.character[i] != second.character[i]) mismatch = TRUE; else i++; } return (! mismatch); } } String::String( ) { current_length = 0; } String::String(const char a[]) { int i; for (i = 0; (i < MAX_STRING_LENGTH && a[i] != '\0'); i++) character[i] = a[i]; current_length = i; } int String::length( ) const { return (current_length); } //uses stdlib.h: char String::one_char(int position) const { if ( (position >= current_length) || (position < 0) ) { cout << "ERROR: illegal position in one_char.\n"; exit(1); } return (character[position]); } //uses stdlib.h: void String::set_char(int position, char new_char) { if ( (position > current_length) || (position < 0) || (position >= MAX_STRING_LENGTH) ) { cout << "ERROR:" << " illegal position in set_char.\n"; exit(1); } character[position] = new_char; if (position == current_length) current_length++; } //Uses iostream.h and ctype.h: istream& operator >>(istream& ins, String& the_string) { char next; ins >> next;//Note: This skips over any initial whitespace. for (int i = 0; (!isspace(next)) && (i < MAX_STRING_LENGTH); i++) { the_string.character[i] = next; ins.get(next); } the_string.current_length = i; //if entire word does not fit, //then discard the part of the word that does not fit: while(!isspace(next)) ins.get(next); return ins; } //Uses iostream.h: ostream& operator <<(ostream& outs, const String& the_string) { for (int i = 0; i < the_string.current_length; i++) outs << the_string.character[i]; return outs; } //Uses iostream.h: void String::input_line(istream& ins) { char next; int i; cin.get(next); for (i = 0; (next != '\n') && (i < MAX_STRING_LENGTH); i++) { character[i] = next; ins.get(next); } current_length = i; while (next != '\n') ins.get(next); }