Given a word the task is to print the frequency of letter occurring in the word.
Example:
“Onion”
The expected output is:-
| Letter | Frequency |
| i | 1 |
| n | 2 |
| O | 1 |
| o | 1 |
Note: Frequency is case sensitive.
Java
import java.util.*;
public class LetterFrequencyW
{
public static void main(String args[])
{
String str="";
char ch=' ';
int l=0,uF=0,lF=0;
Scanner sc=new Scanner(System.in);
System.out.print("Enter a word: ");
str=sc.next();
str=str.trim();
l=str.length();
System.out.println("Letter\tFrequency");
for(char i='A';i<='Z';i++)
{
for(int j=0;j<l;j++)
{
ch=str.charAt(j);
if(ch==i)
{
uF++;
}
if(ch==(i+32))
{
lF++;
}
}
if(uF>0)
{
System.out.println(i+"\t"+uF);
}
if(lF>0)
{
System.out.println((char)(i+32)+"\t"+lF);
}
uF=0;
lF=0;
}
}
}Java