//---------------------------------------------------------------------------- #include #include #include #include #include "stringclass.h" //---------------------------------------------------------------------------- //----Friends //---------------------------------------------------------------------------- istream& operator >>(istream& InputStream,string& AString) { InputStream >> AString.TheCString; return(InputStream); } //---------------------------------------------------------------------------- ostream& operator <<(ostream& OutputStream,const string& AString) { OutputStream << AString.TheCString; return(OutputStream); } //---------------------------------------------------------------------------- //----Public parts //---------------------------------------------------------------------------- //----Constructor with specified length string::string(int Size) { TheCString = new char[Size + 1]; TheCStringLength = Size; TheCString[0] = '\0'; } //---------------------------------------------------------------------------- //----Initial value constructor string::string(const char* StringLiteral) { TheCStringLength = strlen(StringLiteral); TheCString = new char[TheCStringLength + 1]; strcpy(TheCString,StringLiteral); } //---------------------------------------------------------------------------- //----Copy constructor string::string(const string& AString) { TheCStringLength = AString.TheCStringLength; TheCString = new char[TheCStringLength]; strcpy(TheCString,AString.TheCString); } //---------------------------------------------------------------------------- //----Destructor string::~string(void) { delete [] TheCString; } //---------------------------------------------------------------------------- //----Concatenate two strings regardless void string::operator +=(const string& AString) { //----Maybe need to keep track of old C string char* TemporaryPointer = TheCString; //----If enough space then keep it there if (strlen(TheCString) + strlen(AString.TheCString) <= TheCStringLength) strcat(TheCString,AString.TheCString); else { //----Make new memory space TheCStringLength = strlen(TheCString) + strlen(AString.TheCString); TheCString = new char[TheCStringLength + 1]; strcpy(TheCString,TemporaryPointer); strcat(TheCString,AString.TheCString); //----Delete the old memory delete [] TemporaryPointer; } } //---------------------------------------------------------------------------- //----Checks if first string is lexicographically greater than the second boolean string::operator >(const string& AString) const { return(strcmp(TheCString,AString.TheCString) > 0); } //---------------------------------------------------------------------------- //----Checks if first string is the same as the second boolean string::operator ==(const string& AString) const { return(strcmp(TheCString,AString.TheCString) == 0); } //---------------------------------------------------------------------------- //----Checks if first string is lexicographically smaller than the second boolean string::operator <(const string& AString) const { return(strcmp(TheCString,AString.TheCString) < 0); } //---------------------------------------------------------------------------- //----Assignment operator for constants string& string::operator =(const string& AString) { //----Meybe need to keep track of old C string char* TemporaryPointer = TheCString; //----If enough space then just copy across if (strlen(AString.TheCString) <= TheCStringLength) strcpy(TheCString,AString.TheCString); else { //----Make new memory space TheCStringLength = strlen(AString.TheCString); TheCString = new char[TheCStringLength + 1]; strcpy(TheCString,AString.TheCString); //----Delete the old memory delete [] TemporaryPointer; } return(*this); } //---------------------------------------------------------------------------- //----String length int string::Length(void) const { return(strlen(TheCString)); } //---------------------------------------------------------------------------- //----Test if all characters meet the test function boolean string::CharsAreAll(boolean (*TestFunction)(int)) const { int Index = 0; //----Look at each character with the function while (TheCString[Index] != '\0' && (*TestFunction)(TheCString[Index])) Index++; //----Success if reached end of string return(TheCString[Index] == '\0'); } //----------------------------------------------------------------------------