// mostFrequentLine.C // // Written 19 Feb 1996 by Max Hailperin . // // Reads lines of text from standard input until end of file, then // outputs the number of occurances of the most frequently occuring line, // as well as that line itself. #include #include "StringBuilder.h" #include "StringToIntTable.h" static char *getLine(istream &in){ static StringBuilder sb; char c; char *string; while(in.get(c) && c != '\n') sb.add(c); string = sb.string(); if(string[0] == '\0' && !in){ delete [] string; return 0; } else{ return string; } } int main(){ StringToIntTable table; char *mostFrequentLine = 0; int frequency = 0; int newFrequency; char *newLine; while((newLine = getLine(cin)) != 0){ newFrequency = ++table[newLine]; if(newFrequency > frequency){ frequency = newFrequency; delete [] mostFrequentLine; mostFrequentLine = newLine; } else { delete [] newLine; } } if(mostFrequentLine == 0){ cerr << "No lines!\n"; return 1; } else { cout << frequency << ": " << mostFrequentLine << "\n"; } return 0; }