Posted by: alece September 25, 2015
C++
Login in to Rate this Post:     0       ?        
Here, this works assuming your text file has no spaces between words..
So, modify accordingly.Seriously, can't sit here all day and debug your code.
#include "stdafx.h"
#include <fstream>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

string mostFrequent(string input)
{
int max = 0;
int count = 0;
string mostOccuringChar;
for(char a=' ';a<='~';a++)

{
count = 0;
for(int i=0; i) {
if(input[i]==a)
count++;
}

if(count == max)
{
mostOccuringChar += a;
}

if(count>max)
{
max=count;
mostOccuringChar=a;
}
}

return mostOccuringChar;
}


int main()
{
fstream f("C:\\test.txt", fstream::in );
string s;
getline( f, s, '\0');
string test = s;
cout << s << endl;
cout <<"Most frequent is-->"<< mostFrequent(test) <<endl;
f.close();
system("pause");
}
Last edited: 25-Sep-15 02:36 PM
Last edited: 25-Sep-15 02:40 PM
Read Full Discussion Thread for this article