/////////////////////////////////////////////////////////// // // cstring.cpp // // The implementation of a linked-list based // character string class. // // Revision history: // 1/3/00 (Jeff Ondich) Filled in a few functions. // and commented the rest. // /////////////////////////////////////////////////////////// #include "cstring.h" // Initializes the string to empty. CString::CString() { // Using 'Q' here (rather than, say, '\0') // makes it easier to spot one-off errors // involving the head node. mHead.mData = 'Q'; mHead.mNext = NULL; } // Initializes the string using the given // null-terminated string as initial data. CString::CString( const char *str ) { // See above. mHead.mData = 'Q'; // This code or a variant of it will be useful in operator=, too. StringNode *pCurrentNode = &mHead; for( int i=0; str[i] != '\0'; i++ ) { pCurrentNode->mNext = new StringNode; if( pCurrentNode->mNext == NULL ) { cerr << "Allocation error in CString::CString(const char *str), " << "str = \"" << str << "\"" << endl; exit( 0 ); } pCurrentNode = pCurrentNode->mNext; pCurrentNode->mNext = NULL; pCurrentNode->mData = str[i]; } } // Copy constructor for CString. // Initializes the string using the given // CString string as initial data. CString::CString( const CString& s ) { // Stub--init to empty. mHead.mData = 'Q'; mHead.mNext = NULL; } // Destructor for CString. // Be careful here. It's very easy to dereference // a NULL pointer while de-allocating the nodes. CString::~CString() { // Stub. Very important to delete all memory. } // Returns the length, in characters (or, equivalently, // in nodes) of the CString. Does *not* include the // head node. int CString::Length() const { int length = 0; StringNode *pCurrentNode = mHead.mNext; while( pCurrentNode != NULL ) { length++; pCurrentNode = pCurrentNode->mNext; } return( length ); } // Changes all letters in the CString to upper case. void CString::MakeUpper() { // Stub. } // Changes all letters in the CString to lower case. void CString::MakeLower() { // Stub. } // Reverses the order of the non-head nodes in the // CString. void CString::Reverse() { // Stub. } // If the given character is contained in the // CString, Find returns the first (0-based) index at which the // character appears. Otherwise, Find returns -1. int CString::Find( char c ) const { // Stub. return( -1 ); } // If the given null-terminated string is contained in the // CString, Find returns the first (0-based) index at which // str appears. Otherwise, Find returns -1. int CString::Find( const char *str ) const { // Stub. return( -1 ); } // If the given null-terminated string is contained in the // CString, Find returns the first (0-based) index at which // str appears. Otherwise, Find returns -1. int CString::Find( const CString& s ) const { // Stub. return( -1 ); } // If index is >= 0 and < length of the CString, operator[] // returns a reference to the character at the given 0-based // index. Otherwise, operator[] returns a reference to the // character in the head node. char& CString::operator[]( int index ) { // Stub. return( mHead.mData ); } // Copies the given CString to the CString. // ****This requires that you first delete the current // contents of the CString, and then allocate new nodes // to hold the copy. (If you want to be clever, you // can reuse the old nodes with the new data.)**** CString& CString::operator=( const CString& s ) { // Stub. return( *this ); } // Copies the given CString to the CString. // ****This requires that you first delete the current // contents of the CString, and then allocate new nodes // to hold the copy. (If you want to be clever, you // can reuse the old nodes with the new data.)**** CString& CString::operator=( const char *str ) { // Stub. return( *this ); } // Prints the given CString to the given ostream. // Because operator<< is declared to be a friend of // CString, it is allowed access to CString's private // data. ostream& operator<<( ostream& out, const CString& s ) { StringNode *pCurrentNode = s.mHead.mNext; while( pCurrentNode != NULL ) { out << pCurrentNode->mData; pCurrentNode = pCurrentNode->mNext; } return( out ); } // Extracts a word (i.e. a maximal contiguous block of // non-space characters, where space characters are those // for which the return value of isspace is true) // from the given istream and puts it into the given // CString. // // Because operator<< is declared to be a friend of // CString, it is allowed access to CString's private // data. istream& operator>>( istream& in, CString& s ) { // Stub. return( in ); }