Letter Frequency: Write a program to compute the frequency of each lowercase letter in the string.
Input and Output Format:
Input consists of a string. Assume that all characters in the string are lowercase letters and the maximum length of the string is 200.
The letters are displayed sorted in ascending order.
Refer sample input and output for formatting specifications.
All text in bold corresponds to input and the rest corresponds to output.
Sample Input and Output:
Enter the input string
anitha
The letter frequency is
a 2
h 1
i 1
n 1
t 1
Solution
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
int ci, i, j, k, l=0;
String str, str1;
char c, ch;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the input string");
str = sc.nextLine();
System.out.println("The letter frequency is");
i = str.length();
for (c='A'; c<='z'; c++){
k=0;
for (j=0; j<i; j++){
ch = str.charAt(j);
if (ch == c){
k++;
}
}
if (k>0){
System.out.println(c + " " + k);
}
}
}
}
Happy Learning – If you require any further information, feel free to contact me.
Earlier I thought differently, thanks for the help in this question.