WordCountT.cpp

URL: https://mirkwood.cs.edinboro.edu/~bennett/class/cmsc2040/fall2026/notes/words/code/WordCountT.cpp
 
#include <iostream>
#include "WordCountT.h"

using namespace std;
/*

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

WordCountT::WordCountT(){
     word = "";
     count = 0;
}

string Strip(const string & word) {
    string newWord;

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

WordCountT::WordCountT(const std::string & newWord){
     // come back and clean up the word
     word = Strip(newWord);
     count = 1;
}

void WordCountT::Increment(size_t value){
     count += value;
}

void WordCountT::Add(const WordCountT & other){
     count += other.count;
}

WordCountT WordCountT::operator +(const WordCountT & other) const{
     WordCountT newWord;
     newWord.word = word;
     newWord.count = count + other.count;

     return newWord;
}


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

bool WordCountT::operator < (const WordCountT & other) const{
     return word < other.word;
}

bool WordCountT::CompareCount (const WordCountT & other) const{
     return count < other.count;
}


std::string WordCountT::Word() const{
     return word;
}

size_t WordCountT::Count() const{
     return count;
}

std::ostream & operator << (std::ostream & s, const WordCountT  & other){
     s << '"' << other.Word() << "\" : " << other.Count();
     return s;
}