Write a program in java to print the frequency of each character of the string.
THE FOLLOWING PROGRAM SEGMENT SHOWS HOW TO PRINT THE FREQUENCY OF EACH CHARACTER OF THE STRING.
import java.util.*;
class frequency
{
public static void main (String args[])
{
Scanner ob = new Scanner (System.in);
System.out.println("Enter any STRING");
String word = ob.nextLine();
int l = word.length();
word = word.toUpperCase();
int a[] = new int [26];
for(int i = 0; i<26 ; i++)
{
if (word.charAt(i) >= 65 && word.charAt(i) <= 90)
{
a[word.charAt(i) - 65]++;
}
}
System.out.println("CHARACTERS \t FREQUENCY");
for (int i = 0 ; i <26; i++)
{
if(a[i] != 0)
{
System.out.println((char)(i +65) + "\t" + a[i]);
}
}
}
}
Hope you guys found it helpful. Please share it more with your friends and comment down in the comment section below.
Thank you!!!
Please visit our web page https://askrisfor.blogspot.com/ for more such information.
Comments
Post a Comment