Given an array of strings, write a function mostFrequentStrings(String[] in) usi
ID: 3756105 • Letter: G
Question
Given an array of strings, write a function mostFrequentStrings(String[] in) using your implementation of Class HashtableChaining that computes the frequency of each string and returns an array of strings consisting of the 5 most frequent strings. You have to do this using the provided Pair class. Again, the strings will be combinations of lowercase and/or uppercase characters delimited by a newline character.
class Pair<K,V>
{
/*
The Pair class is intended to store key, value pairs. It'll be helpful
for part 1.2 of the assignment.
*/
public K key;
public V value;
public Pair(K key, V value)
{
this.key = key;
this.value = value;
}
}
Explanation / Answer
import java.util.Scanner;
public class JavaProgram
{
public static void main(String args[])
{
int ci, x, y, k, l=0;
String s1, s2;
char ch, chr;
Scanner scan = new Scanner(System.in);
System.out.print("Enter a String : ");
s1=scan.nextLine();
x=s1.length();
for(ch='A'; ch<='z'; ch++)
{
k=0;
for(y=0; y<x; y++)
{
chr = s1.charAt(y);
if(chr == ch)
{
k++;
}
}
if(k>0)
{
System.out.println("The character " + ch + " has occurred for " + k + " times");
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.