Write a program to accept a string and print the frequency of letters in the given format.

I/p I love my India

O/p

Frequency of characters in the string

A 1
D 1
E 1
I 3
L 1
M 1
N 1
O 1
V 1
Y 1

import java.util.*;
class Frequency_of_characters{
    public static void main(){
        Scanner ob=new Scanner(System.in);
        System.out.println("Enter a String");
        String str=ob.nextLine();
        str=str.toUpperCase();
        System.out.println("Frequency of characters in the string \n");
        int l=str.length();
        int f=0;
        for(int i=65;i<=90;i++){
            char ch=(char)i;
            for(int j=0;j<l;j++){
                if(str.charAt(j)==ch){
                    f++;
                }
            }
            if(f!=0){
                System.out.println(ch+" \t"+f);
            }
            f=0;
        }
    }
}

LEAVE A REPLY

Please enter your comment!
Please enter your name here