You are on page 1of 3

Blue Ridge Public School

Class X – Computer Applications

A.Y. 2023-24

ASSIGNMENT – 20

1. Write a program to accept a sentence from the user, convert it to


uppercase and print the frequency of each character.

Sample Input: computer hardware


Sample Output: Char 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

import java.util.*;
public class Frequency
{
public void findFrequency()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a string: ");
String str = sc.nextLine();
str = str.toUpperCase();
int count = 0;
char ch1;
System.out.println("Char" + "\t" + "Frequency");
for(char ch = 'A'; ch <= 'Z'; ch++)
{
count = 0;
for(int j = 0; j < str.length() ; j++)
{
ch1 = str.charAt(j);
if(ch == ch1)
count++;
}
if(count != 0)
{
System.out.println(ch + "\t" + count);
}
}
}
public static void main(String [] args)
{
Frequency obj = new Frequency();
obj.findFrequency();
}
}
OUTPUT

Enter a string:
computer applications
Char Frequency
A 2
C 2
E 1
I 2
L 1
M 1
N 1
O 2
P 3
R 1
S 1
T 2
U 1
Variable Name Data type Description

str String stores String values

ch, ch1 char stores character values

j, count int stores integer values

You might also like