-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFreqCount.java
More file actions
124 lines (90 loc) · 2.79 KB
/
FreqCount.java
File metadata and controls
124 lines (90 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
public class FreqCount {
final private int a_ASCII = 97,z_ASCII = 97+25, ALPHABET_LENGTH = 26;
private char alphabet[];
private int letterCount[];
private double percentCount[];
//Runs through all the methods
public void analyse(String s) {
alphabet = buildAlphabet();
letterCount = freqCount(s);
percentCount = percentageCount(s, letterCount);
sortArrays();
printArr(letterCount, percentCount);
}
//Sorts the three main arrays in descending order
private void sortArrays() {
int lowest = 0, currentIndex = 0;
for(int i = 0; i<ALPHABET_LENGTH; i++){
for(int j = currentIndex; j<ALPHABET_LENGTH; j++) {
if(letterCount[lowest] <letterCount[j])
lowest = j;
}
swap(lowest, currentIndex);
currentIndex++;
lowest = currentIndex;
}
}
//Helper method to above, does the swapping
private void swap(int lowest, int currentIndex) {
//Make temp
char x = alphabet[lowest];
int y = letterCount[lowest];
double z = percentCount[lowest];
//Swap half the stuff
alphabet[lowest] = alphabet[currentIndex];
letterCount[lowest] = letterCount[currentIndex];
percentCount[lowest] = percentCount[currentIndex];
//Swap the other half
alphabet[currentIndex] = x;
letterCount[currentIndex] = y;
percentCount[currentIndex] = z;
}
//counts the number of times each letter appears
private int[] freqCount(String s) {
int count[] = new int[ALPHABET_LENGTH];
s = s.toLowerCase();
for(int i = 0; i<s.length();i++) {
char current = s.charAt(i);
if(current >= a_ASCII && current <=z_ASCII) {
count[current - a_ASCII] +=1;
}
}
return count;
}
//Takes the previous methods return value and calculates the percentage of each letter
private double[] percentageCount(String s, int n[]) {
double stringLength = s.length() - countSpaces(s);
double freq[] = new double[26];
for(int i = 0;i < n.length;i++) {
freq[i] =(n[i]/stringLength)*100;
}
return freq;
}
//Helper method to the above method, counts the number of spaces
private int countSpaces(String s) {
int count = 0;
char char_arr[] = s.toCharArray();
for(char c:char_arr)
if(c == ' ')
count++;
return count;
}
//Prints the three main arrays
private void printArr(int n[], double n2[]) {
System.out.println("Letter\tCount\tPercentage");
for(int i = 0; i< ALPHABET_LENGTH; i++)
{
System.out.printf("%c\t%d\t%.2f\n", alphabet[i], n[i], n2[i]);
}
}
//returns a char array of letters a-z
private char[] buildAlphabet() {
char n[] = new char[ALPHABET_LENGTH];
char x = 96;
for(int i = 0; i< n.length; i++) {
x++;
n[i] = x;
}
return n;
}
}