WordCounterT.cpp

URL: http://147.64.242.52/~bennett/class/cmsc2040/fall2026/notes/templates/code/WordCounterT.cpp
 
#include <iostream>
#include <string>
#include "WordCounterT.h"

/*
   private:
     std::string word;
     int count{0};
*/

using namespace std;

WordCounterT::WordCounterT() {}

string CleanString(string word) {
   string rv;
   for(size_t i =0; i < word.size(); ++i) {
      if (isalpha(word[i])) {
         rv = rv + static_cast<char>(tolower(word[i]));
      }
   }

   return rv;
}

WordCounterT::WordCounterT(std::string newWord){
    word = CleanString(newWord);
    if (word != "") {
       count = 1;
    }
}

std::string WordCounterT::GetWord() const{
   return word;
}

int WordCounterT::GetCount() const{
   return count;
}


bool WordCounterT::operator == (const WordCounterT & other) const{
    return word == other.word;
}

WordCounterT & WordCounterT::operator ++(){
     ++count;
     return *this;
}

WordCounterT WordCounterT::operator ++(int){
    WordCounterT tmp{*this};
    ++count;
    return tmp;
}

std::ostream & operator << (ostream & s, const WordCounterT & word){
    s << word.GetWord() << ":" << word.GetCount();
    return s;
}