Question
Print character with maximum frequency and frequency of all characters in given sentence.
Enter a sentence:
the quick brown fox jumped over the lazy dog.
A:1
B:1
C:1
D:2
E:4
F:1
G:1
H:2
I:1
J:1
K:1
L:1
M:1
N:1
O:4
P:1
Q:1
R:2
S:0
T:2
U:2
V:1
W:1
X:1
Y:1
Z:1
Character with maximum frequency in given sentence:E
Share code with your friends
Share on whatsapp
Share on facebook
Share on twitter
Share on telegram
Code
import java.util.Scanner;
public class FrequencyOfCharacters
{
public static void main(String[] args)
{
String sen="";
char ch=' ',ch1=' ',maximumFrequencyCharacter=' ';
int i=0,len=0,count=0,maxFrequency=0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter a sentence:");
sen = sc.nextLine();
sen=sen.toUpperCase();
len=sen.length();
for(ch='A';ch<='Z';ch++)
{
count=0;
for(i=0;i< len;i++)
{
ch1=sen.charAt(i);
if(ch1==ch)
{
count++;
}
}
if(count>maxFrequency)
{
maxFrequency=count;
maximumFrequencyCharacter=ch;
}
System.out.println(ch+":"+count);
}
System.out.println("Character with maximum frequency in given sentence:"+maximumFrequencyCharacter);
}
}