Write a program that count the frequency of letters in a paragraph in java langu
ID: 3752002 • Letter: W
Question
Write a program that count the frequency of letters in a paragraph in java language.After that replace the most frequent letter with letter “e”.
The output will be the same as the input structure ( spaces , numbers .. etc ) it only can replace letters with the programmer choice..
It is like to crack the substitution cipher does..
Write a program that count the frequency of letters in a paragraph in java language.
After that replace the most frequent letter with letter “e”.
The output will be the same as the input structure ( spaces , numbers .. etc ) it only can replace letters with the programmer choice..
It is like to crack the substitution cipher does..
Explanation / Answer
import java.util.*;
class frequency {
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
String s;char c;
System.out.println("Enter the Paragraph:");
s=sc.nextLine();
System.out.println("Enter the character with which you want to replace:");
c=sc.next().charAt(0);
int l=s.length();
char d,m=' ';int count=0;int max=-1;
for(char a='a';a<='z';a++)
{
for(int i=0;i<l;i++)
{
d=s.charAt(i);
if((a==Character.toLowerCase(d)) || (a==Character.toUpperCase(d)))
{
count++;
}
}
if(count>max)
{
max=count;
m=a;
}
count=0;
}
System.out.println("The letter with maximum frequency is: "+m+" and it's count is: "+max);
s=s.replace(m,c);
System.out.println("The replaced Paragraph is: "+s);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.