#include #include #include /* Class STRING version 2 This is a dynamic array version of the STRING class. Beyond implementing a string data type, this class is designed to demonstrate dynamic array management. */ class STRING { friend ostream &operator<<(ostream &, const STRING &); friend istream &operator>>(istream &, STRING &); public: //Constructors STRING(); STRING(char *); STRING(const STRING &); //Destructor ~STRING(); //Public Member Functions void cat(const STRING &); void concat(const STRING &); void cat(STRING &, STRING &); void concat(STRING, STRING); void copy(const STRING &); int len(); int length(); int compare(const STRING &); //Operators int operator==(const STRING &) const; int operator!=(const STRING &) const; int operator<(const STRING &) const; int operator<=(const STRING &) const; int operator>(const STRING &) const; int operator>=(const STRING &) const; char *operator+(const STRING &); void operator+=(const STRING &); private: //Private Data Members char *data; int size; }; //Constructor Definition STRING::STRING() { size = 0; } STRING::STRING(char *x) { size = strlen(x); data = new char[size + 1]; strcpy(data, x); } STRING::STRING(const STRING &x) { size = x.size; strcpy(data, x.data); } //Deconstructor STRING::~STRING(){delete data;} //Public Member Function Definitions void STRING::cat(const STRING &x) { char *tmp; size += x.size; tmp = new char[size + 1]; strcat(strcpy(tmp, data), x.data); delete data; data = tmp; } void STRING::concat(const STRING &x) { char *tmp; size += x.size; tmp = new char[size + 1]; strcat(strcpy(tmp, data), x.data); delete data; data = tmp; } void STRING::cat(STRING &x, STRING &y) { char *tmp; size = x.size + y.size; tmp = new char[size + 1]; strcat(strcpy(tmp, x.data), y.data); delete data; data = tmp; } void STRING::concat(STRING x, STRING y) { char *tmp; size = x.size + y.size; tmp = new char[size + 1]; strcat(strcpy(tmp, x.data), y.data); delete data; data = tmp; } void STRING::copy(const STRING &x) { size = x.size; if(size != 0) { delete data; } data = new char[size + 1]; } int STRING::len() {return size;} int STRING::length() {return size;} int STRING::compare(const STRING &x) { return strcmp(data, x.data);} //Operator Definition ostream &operator<<(ostream &out, const STRING &x) { out << x.data; return out; } istream &operator>>(istream &in, STRING &x) { char tmp[1024]; if( x.size != 0) delete x.data; in >> setw(1024) >> tmp; x.size = strlen(tmp) + 1; strcpy(x.data, tmp); return in; } int STRING::operator==(const STRING &x) const {return strcmp(data,x.data) == 0;} int STRING::operator!=(const STRING &x) const {return strcmp(data,x.data) != 0;} int STRING::operator<(const STRING &x) const {return strcmp(data,x.data) < 0;} int STRING::operator<=(const STRING &x) const {return strcmp(data,x.data) <= 0;} int STRING::operator>(const STRING &x) const {return strcmp(data,x.data) > 0;} int STRING::operator>=(const STRING &x) const {return strcmp(data,x.data) >= 0;} char *STRING::operator+(const STRING &x) { int nsize; nsize = size + x.size; char *tmp = new char[nsize + 1]; strcat(strcpy(tmp,data),x.data); return tmp; } void STRING::operator+=(const STRING &x) { cat(x); }