// Word.cpp

#include <assert.h>
#include "Word.h"

class Word;

// constructor
Word::Word( char s[] )
{
	int i;
	for ( i = 0; s[i] != '\0' && i < WORD_LENGTH - 1; i++ )
		aWord[i] = s[i];
	aWord[i] = '\0';
}

// returns the length of a word
int Word::length()
{
	int i = 0;
	while ( aWord[i] != '\0' )
		i++;
	return i;
}

int Word::inRange( int index )
{
	if ( index < 0 || index > strlen( aWord ) )
		throw( index );
	return aWord[index];
} 

// overloading operator ==
bool operator == ( Word w1, Word w2 )
{
	return::strcmp( w1.aWord, w2.aWord ) == 0;
}

char& Word::operator[] ( int index )
{
	assert( inRange( index ) );
	return aWord[index];
}

// prints a word
void Word::print( ostream & out )
{
	out << aWord;
}

// reads a word
void Word::read( istream & in )
{
	in >> aWord;
}

//overloading operator << to print a word
ostream & operator << ( ostream & out, Word w )
{
	w.print( out );
	return out;
}

//overloading operator >> to read in a word
istream & operator >> ( istream & in, Word w )
{
	w.read( in );
	return in;
}