Problems

Frequency counter implementation

#include <iostream>
#include "Symbol Table.h"

int main()
{
     ST<string, int> *st = new ST<string, int>(5);
     int minLen;
     cout << "Minimum Length: ";
     cin >> minLen;

     while (true)
     {
          string word;
          cin >> word;
          if (word == "-")
               break;
          if (word.length() < minLen) // ignore short strings
               continue;
          if (!st->contain(word))
               st->put(word, 1);
          else
               st->put(word, st->get(word) + 1); // read string and update frequency
     }

     // print a sting with most frequency
     string max = "";
     st->put(max, 0);
     for (int i = 0; i < st->size(); i++)
     {
          if (st->get(st->getKey(i)) > st->get(max))

               max = st->getKey(i);
     }
     cout << "Most Frequest: " << max << " : " << st->get(max);
}

Last updated

Was this helpful?