Find Character's Frequency
package com.talent;
public class Find_Character_Frequency {
static int [] characterFrequency(String s)
{
s=s.toLowerCase();
int frequency[]=new int[26];
for (int i = 0,c=97; i < 26; i++,c++)
{
for (int j = 0; j < s.length(); j++)
{
char ch=s.charAt(j);
if(ch==c)
frequency[i]++;
} }
return frequency;
}
public static void main(String[] args) {
String s="I am Java Developer";
int frequency[]=characterFrequency(s);
System.out.println("Albhabet \t Frequency");
for (int i = 0,c=97; i <26; i++,c++) {
if(frequency[i]!=0){
char ch=(char)c;
System.out.println(ch+"\t\t"+frequency[i]);
}
}
}
}
OutPut->
Albhabet Frequency
a 3
d 1
e 3
i 1
j 1
l 1
m 1
o 1
p 1
r 1
v 2
Albhabet Frequency
a 3
d 1
e 3
i 1
j 1
l 1
m 1
o 1
p 1
r 1
v 2
No comments:
Post a Comment