Programming Examples

Java program to input a string in uppercase and print the frequency of each character


Write a program to input a string in uppercase and print the frequency of each character.

Example:

INPUT : COMPUTER HARDWARE

OUTPUT:

CHARACTER FREQUENCY

A 2

C 1

D 1

E 2

H 1

M 1

O 1

P 1

R 3

T 1

U 1

W 1

Solution

import java.util.*;
class Frequency
{
    public static void main(String arr[])
    {
        char ch;
        String str;
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter any String ");
        str=sc.nextLine();
        str=str.toUpperCase();
        System.out.println("CHARACTER\tFREQUENCY");
        for(ch='A';ch<='Z';ch++)
        {
            int count=0;
            for(int a=0;a<str.length();a++)
            {
                if(str.charAt(a)==ch)
                    count++;
            }
            if(count>0)
            {
                    System.out.println(ch+"\t\t"+count);
            }
        }
    }
}
Output

Enter any String 

COMPUTER HARDWARE

CHARACTER FREQUENCY

A 2

C 1

D 1

E 2

H 1

M 1

O 1

P 1

R 3

T 1

U 1

W 1

CCC Online Test 2021 CCC Practice Test Hindi Python Programming Tutorials Best Computer Training Institute in Prayagraj (Allahabad) Best Java Training Institute in Prayagraj (Allahabad) Best Python Training Institute in Prayagraj (Allahabad) O Level Online Test in Hindi Bank SSC Railway TET UPTET Question Bank career counselling in allahabad Sarkari Naukari Notification Best Website and Software Company in Allahabad Sarkari Exam Quiz