count each character

Write a java program to count frequency of each char using Array :-


import java.util.Scanner;
class CharFreq{
static Scanner sc=new Scanner(System.in);
public static void main(String[] args) {
System.out.println("Enter a String : ");
String str=sc.nextLine();
int fr[]=new int[26];
for(int i=0;i<str.length();i++) {
if(str.charAt(i)>=65 && str.charAt(i)<=90) {
fr[str.charAt(i)-65]++;
}
else if(str.charAt(i)>=97 && str.charAt(i)<=122) {
fr[str.charAt(i)-97]++;
}
}
for(int i=0;i<26;i++) {
if(fr[i]>0)
System.out.println((char)(i+65)+" | "+fr[i]);
}

}
}

Comments

Post a Comment

Popular posts from this blog