//string.h #include <iostream> using namespace std; class String; istream& operator>>( istream&, String& ); ostream& operator<<( ostream&, String& ); class String { public: //String str1; //String str2( "literal" ); //String str3( str2 ); String(); String( const char* ); String( const String& ); ~String(); bool operator==( const String& ) const; bool operator==( const char* ) const; bool operator!=( const String& ) const; String& operator=( const String& ); String& operator=( const char* ); char& operator[]( const int ); int size() { return _size; } char* c_str() { return _string; } private: char *_string; int _size; };
//string.cpp #include "string.h" // to include strcmp // in the C standard library #include <cstring> #include <iostream> #include <cassert> #include <iomanip> using namespace std; bool String::operator== ( const String &rhs ) const { if ( _size != rhs._size ) return false; else return strcmp( _string, rhs._string) ? false : true; } bool String::operator==( const char *c ) const { return strcmp( _string, c ) ? false : true; } String::String() { _size = 0; _string = 0; } String::String( const String &rhs ) { _size = rhs._size; if ( !rhs._string ) { _string = 0; } else { _string = new char[_size + 1]; strcpy( _string, rhs._string ); } } String::String( const char *s ) { if ( !s ) { _size = 0; _string = 0; } else { _size = strlen( s ); _string = new char[_size + 1]; strcpy( _string, s ); } } String::~String() { delete [] _string; } String& String::operator= ( const String &rhs ) { if ( this != &rhs ) { _size = rhs._size; delete [] _string; if ( !rhs._string ) _string = 0; else { _string = new char[_size + 1]; strcpy( _string, rhs._string ); } } else return *this; } String& String::operator= ( const char *c ) { if ( !c ) { _size = 0; delete [] _string; _string = 0; } else { //cout << "operator= invoked for char: " << *c << endl; _size = strlen( c ); delete [] _string; _string = new char[_size + 1]; strcpy( _string, c ); } } char& String::operator[](const int index) { assert( index >= 0 && index < _size); return _string[ index ]; } istream& operator>>( istream& io, String &s) { const int limit_string_size = 4096; char inBuf[ limit_string_size ]; io >> setw( limit_string_size ) >> inBuf; s = inBuf; //String::operator=( const char* ) return io; } ostream& operator<<( ostream& os, String &s) { os << s.c_str(); }
//stringtest.cpp #include "string.h" #include <iostream> using namespace std; int main() { int aCnt = 0, eCnt = 0, iCnt = 0, oCnt = 0, uCnt = 0, theCnt = 0, itCnt =0, wdCnt = 0, notVowel = 0; String buf, the( "the" ), it( "it" ); // invoke operator>> while ( cin >> buf ) { ++wdCnt; //invoke operator<< cout << buf << ' '; if ( wdCnt%12 == 0 ) cout << endl; if ( buf == the || buf == "The" ) ++theCnt; else if ( buf == it || buf == "It" ) ++itCnt; for ( int ix = 0; ix < buf.size(); ++ix ) { switch ( buf[ ix ] ) { case 'a': case 'A': ++aCnt; break; case 'e': case 'E': ++eCnt; break; case 'i': case 'I': ++iCnt; break; case 'o': case 'O': ++oCnt; break; case 'u': case 'U': ++uCnt; break; default: ++notVowel; break; } } } cout << " " << "Words read: " << wdCnt << " " << "the/The: " << theCnt << ' ' << "it/It: " << itCnt << " " << "non-vowel read: " << notVowel << " " << "a: " << aCnt << ' ' << "e: " << eCnt << ' ' << "i: " << iCnt << ' ' << "o: " << oCnt << ' ' << "u: " << uCnt << endl; }