Finding Frequency of Letters in Java

Write a program to enter a word in uppercase and display the frequency of each letter in the word.

Program:

import java.io.*;
class Word{
    public static void main(String args[])
    throws IOException{
        InputStreamReader in = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(in);
        System.out.print("Enter the word: ");
        String w = br.readLine();
        w = w.trim();
        if(w.indexOf(' ') > 0)
            w = w.substring(0, w.indexOf(' '));
        w = w.toUpperCase();
        for(char ch = 'A'; ch <= 'Z'; ch++){
            int count = 0;
            for(int i = 0; i < w.length(); i++){ 
                if(ch == w.charAt(i)) 
                    count++; 
            } 
            if(count > 0)
                System.out.println(ch + " - " + count);
        }
    }
}

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: