Skip to content

Instantly share code, notes, and snippets.

@XoLinA
Created December 17, 2025 19:29
Show Gist options
  • Select an option

  • Save XoLinA/bc9bf7de15422a00e57b6d34ede92ee6 to your computer and use it in GitHub Desktop.

Select an option

Save XoLinA/bc9bf7de15422a00e57b6d34ede92ee6 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <fstream>
#include <map>
#include <string>
using namespace std;
int main()
{
ifstream inFile("input.txt");
ofstream outFile("output.txt");
if (!inFile.is_open())
{
cout << "File not found\n";
return 1;
}
map<string, int> freq;
string word;
while (inFile >> word)
{
freq[word]++;
}
cout << "Result:\n";
outFile << "Result:\n";
map<string, int>::iterator it;
for (it = freq.begin(); it != freq.end(); it++)
{
cout << (*it).first << " : " << (*it).second << endl;
outFile << (*it).first << " : " << (*it).second << endl;
}
string maxWord = "";
int maxCount = 0;
for (it = freq.begin(); it != freq.end(); it++)
{
if ((*it).second > maxCount)
{
maxCount = (*it).second;
maxWord = (*it).first;
}
}
cout << "\Best word: " << maxWord << " count: " << maxCount << endl;
outFile << "\nBest word: " << maxWord << " count: " << maxCount << endl;
inFile.close();
outFile.close();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment