Tuesday, March 8, 2011

WAP to printMostFrequentCharcters..Very Important Question

You are given a function printMostFrequentWords which takes in an array of strings. You are required to print a list of all the letters that occurred with the highest frequency in each line of the file, followed by the frequency.
The list of letters should be an alphabetical list of upper case letters followed by an alphabetical list of lower case letters.
Sample Test Cases:

Input #00:
When riding your bicycle backwards down a one-way street, if the
wheel falls of a canoe, how many ball bearings does it take to fill
up a water buffalo?
Hello Howard.

Output #00:
e 6
al 7
a 3
Hlo 2

Yes This is The Exact Working Code ..Enjoy

import java.util.*;
import java.io.*;

public class ReadFileSystem
{

public static void main(String args[])throws Exception
{

BufferedReader in = new BufferedReader(new FileReader("E:/ubuntu/test.txt"));//the file containing lines.
HashMap lettercount=lettercount = new HashMap();;
String str="";

while ((str = in.readLine()) != null)
{


for (int i = 0; i < str.length(); i++)
{
Character c = str.charAt(i);

if (Character.isLetter(c))
{
if(lettercount.containsKey(c))
{

lettercount.put(c,lettercount.get(c) + new Integer(1));
}
else
{
lettercount.put(c,new Integer(1));
}
}

}


int maxCount = 0;

for(Integer count: lettercount.values())
{
if (count > maxCount)
{
maxCount = count;

}
}

StringBuffer characters =new StringBuffer();

for (Map.Entry entry: lettercount.entrySet())
{
if (entry.getValue().equals(maxCount))
{
if(entry.getKey()>='A' && entry.getKey() <='Z')
{
characters.append(entry.getKey());
characters.reverse();
}
else
characters.append(entry.getKey());

}
}
System.out.print(characters);
System.out.println(" " + maxCount);

//resetting count value
for(Map.Entry entry: lettercount.entrySet())
{
lettercount.put(entry.getKey(),0);

}


}



}

}

Cheers

2 comments :

Unknown said...

hi can you give a code in c for the same problem.

Unknown said...

@Diyya , just try to code it by own , let me know what problem u are facing ?