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