//string2.cpp //methods for class String #include "string2.h" #include #include #include const int Max = 256; //default constructor String::String() { length = 0; val = new char; assert(val); *val = '\0'; } //conversion constructor from a C string String::String(char *s) { assert(s); length = strlen(s); val = new char[length+1]; assert(val); strcpy(val, s); } //destructor String::~String() { delete [] val; } //returns the length of string int String::getlength() { return length; } //creates a copy of string s void String::copy(String *s) { assert(s); length = s->length; delete [] val; val = new char[length+1]; assert(val); strcpy(val, s->val); } //concatenates two strings void String::cat(String *s) { assert(s); char *temp; length += s->length; temp = new char[length+1]; assert(temp); strcpy(temp, val); strcat(temp, s->val); delete [] val; val = temp; } //returns a positive integer if the first String is greater than the second; //returns a negative integer if the first is less than second; //returns 0 if they are equal int String::compare(String *s) { return strcmp(val, s->val); } //returns pointer to nth element of string, if n is outside the //length of string, returns NULL char* String::at(int n) { if (n > length-1 || n < 0) return NULL; return &val[n]; } //prints out string void String::print() { cout<