Frequency Count

 

Lab 1:

Frequency Count Program

===================================

public class FrequencyCount {
public static void main(String[] args) {
String s=”jspidersdeveloper”;
char alph[]=new char[26];//array for storing alphabets from a to z
int freq[]=new int[26];//array for storing frequency of all elements
char c=’a’;
System.out.println(“================”);
System.out.println(“frequency of all characters is…..”);
for(int i=0;i<26;i++)
{ alph[i]=c;//storing all elements from a till z in alph[] array
freq[i]=0;//initialzing the count for every alphabet with 0
c++;
}
/*counting frequency logic */
for(int i=0;i<26;i++)
{ for(int j=0;j<s.length();j++)
{ char ch=s.charAt(j);//extracting char one by one
if(ch==alph[i])
{ freq[i]++;
}
}
if(freq[i]!=0)
System.out.println(” “+alph[i]+”—> “+freq[i]+ ” times”);
}
}
}

 

=========================================================================

Output :

frequency of all characters is…..
d—> 2 times
e—> 4 times
i—> 1 times
j—> 1 times
l—> 1 times
o—> 1 times
p—> 2 times
r—> 2 times
s—> 2 times
v—> 1 times

=========================================================================

 

 

Advertisements

Leave a Reply to assist you..

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s