//word.cc

#include <ctype.h>
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include "Word.h"
#include <stdio.h>

char* Word::strdup(const char* word) {
char* word2 = new char[strlen(word) + 1];
strcpy(word2, word);
return(word2);
}

Word::Word() {
_chars = strdup("");
}

Word::Word(const char* word) {
_chars = strdup(word);
}

Word::Word(char c) {
_chars = new char[2];
_chars[0] = c;
_chars[1] = '\0';
}

Word::Word(int i) {
_chars = new char[10];
sprintf(_chars, "%d", i);
}

Word::Word(const Word& s) {
_chars = strdup(s._chars);
}

Word::~Word(){ delete [] _chars;}

int Word::length() const {
return(strlen(_chars));
}

const char* Word::chars() const {
return (_chars);
}


void Word::setCharAt(int index, char c) {
if (index >= length()) {
cerr << "Program error: invalid index for Word::setCharAt function\n";
exit(1);
}
_chars[index] = c;
}


char Word::charAt(int i) const {
if (i >= length()) {
cerr << "Program error: invalid index for Word::charAt function\n";
exit(1);
}
return(_chars[i]);
}

Word Word::operator + (const Word& s) const {
char* temp = new char[length() + s.length() + 1];
strcpy(temp,_chars);
strcat(temp, s._chars);
Word s2(temp);
delete [] temp;
return(s2);
}

Word Word::operator + (int num) const {
char buf[15];
sprintf(buf, "%d", num);
char* temp = new char[length() + strlen(buf) + 1];
strcpy(temp,_chars);
strcat(temp, buf);
Word s2(temp);
delete [] temp;
return(s2);
}

Word Word::operator + (const char * word) const {
char* temp = new char[length() + strlen(word) + 1];
strcpy(temp,_chars);
strcat(temp, word);
Word s2(temp);
delete [] temp;
return(s2);
}

Word Word::operator + (char c) const {
unsigned int len = length();
char* temp = new char[len + 2];
strcpy(temp,_chars);
temp[len] = c;
temp[len+1] = '\0';
Word s2(temp);
delete [] temp;
return(s2);
}

void Word::upcase() {
for (char *cp = _chars; *cp != '\0'; cp++)
if (islower(*cp)) *cp = toupper(*cp);
}

void Word::downcase() {
for (char *cp = _chars; *cp != '\0'; cp++)
if (isupper(*cp)) *cp = tolower(*cp);
}

Word& Word::operator = (const Word& s) {
if (&s != this) {
delete [] _chars;
_chars = strdup(s._chars);
}
return(*this);
}


Word& Word::operator = (const char * word) {
delete [] _chars;
_chars = strdup(word);
return(*this);
}

Word& Word::operator = (char c) {
delete [] _chars;
_chars = new char[2];
_chars[0] = c;
_chars[1] = '\0';
return(*this);
}
int Word::compare (const Word& s) const {
int result = strcmp(_chars, s._chars);
if (result < 0) return (-1);
else if ( result > 0) return(1);
else return(0);
}

bool Word::operator == (const Word& s) const {
return(compare(s) == 0);
}

bool Word::operator != (const Word& s) const {
return(compare(s) != 0);
}

bool Word::startsWith(const Word& s) const {
char* match = strstr(_chars,s._chars);
return (match == _chars);
}

bool Word::endsWith(const Word& s) const {
int i = length() - 1;
int j = s.length() - 1;

if (j > i)
return (false);

for (; j >= 0 ; i--, j--)
if (_chars[i] != s._chars[j])
return(false);
return(true);
}

bool Word::contains(const Word& s) const {
return(strstr(_chars,s._chars) != NULL);
}

Word Word::subString(int s, int e) const {
int len = length();
if (s >= len || e >= len)
return _chars;

Word newString;
for (int i = s; i <= e; i++)
newString = newString + charAt(i);
return newString;
}