#include #include #include #include #include "string.h" /********************************************************************** * String::String(char []) --- Constructor to be used to initialize a * string object from a char string. **********************************************************************/ String::String(char in[]) { int i; len = strlen(in); // allocate storage space s = new char[len]; assert (s != NULL); for (i = 0; i < len; ++i) // copy the string s[i] = in[i]; } /********************************************************************** * String::~String --- Destructor. Deallocate any dynamic memory. **********************************************************************/ String::~String() { delete [] s; } /********************************************************************** * String::StrLen --- Return the length of the string object. **********************************************************************/ int String::StrLen(void) { return len; } /********************************************************************** * String::StrZero --- Null-out the string, deallocating memory. **********************************************************************/ void String::StrZero(void) { len = 0; delete [] s; s = NULL; } /********************************************************************** * String::StrPrt --- Print a string **********************************************************************/ void String::StrPrt(void) { int i; for (i = 0; i < len; ++i) cout << s[i]; } /********************************************************************** * String::StrCpy --- Copy src to this string object. The previous * string is lost. **********************************************************************/ void String::StrCpy(const String& src) { int i; delete [] s; // Get rid of old string. s = NULL; len = src.len; // Allocate space for copy of src. s = new char[len]; assert (s != NULL); for (i = 0; i < len; ++i) // Copy src. s[i] = src.s[i]; } /********************************************************************** * String::StrCat --- Append src to this string. **********************************************************************/ void String::StrCat(const String& src) { int i; int j; char* tmp; tmp = new char[len + src.len]; // Allocate space for concatenated // strings. assert (tmp != NULL); for (i = 0; i < len; ++i) // Copy original string. tmp[i] = s[i]; for (j = 0; j < src.len; ++j, ++i) // Concatenate src. tmp[i] = src.s[j]; len += src.len; // Update object string. delete [] s; // Delete old string. s = tmp; } /********************************************************************** * String::Strchr --- Search for c in object string. Return position * (1 relative) of c in object or 0 if not found **********************************************************************/ int String::StrChr(char c) { int i = 0; while (i < len && s[i] != c) ++i; if (i == len) return 0; else return i + 1; } /********************************************************************** * String::StrCmp --- Compare object string and cmp. See strstr(1) * for details. **********************************************************************/ int String::StrCmp(const String& cmp) { int i = 0; // find length of shorter string int cmpLen = (len < cmp.len) ? len : cmp.len; while (i < cmpLen && s[i] == cmp.s[i]) ++i; if (i == cmpLen) // reached end of shorter string { if (len == cmp.len) // both strings same length: they're equal return 0; else if (len == cmpLen) // invoked object shorter return -cmp.s[i]; else return s[i]; // argument object shorter } else // found mismatch characters return s[i] - cmp.s[i]; } /********************************************************************** * String::StrStr --- Search for little in this object string (big). * See strstr(1) for details. **********************************************************************/ int String::StrStr(const String& little) { int start; // Offset into big. int i; // Index into little, little. if (little.len == 0) // little is the empty string. return 1; start = 0; while (start < len ) // The usual strstr code. { i = 0; while (start + i < len && i < little.len) { if (s[start + i] != little.s[i]) // Mismatch found. break; ++i; } if (i == little.len) // Found little in big. return start + 1; ++start; } return 0; // Didn't find little in big. }