-
Notifications
You must be signed in to change notification settings - Fork 0
/
frequencyAnalyzer.java
49 lines (38 loc) · 1.21 KB
/
frequencyAnalyzer.java
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
/*
* Frequency analyzer for cryptanalysis.
* jmh
*/
import java.util.Scanner;
public class frequencyAnalyzer {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
if (args.length == 1)
output(countsingle(args[0]));
/* If no args, uses standard input to get text. */
else if (args.length == 0)
output(countsingle(input.next()));
else
System.out.println("Too many arguments");
}
public static int[] countsingle(String text) {
/* Array index out of bounds at 26. No idea why. */
int[] freq = new int[27];
text = text.toLowerCase();
for (int i = 0; i <= text.length() - 1; i++) {
if (Character.isLetter(text.charAt(i))) {
freq[((int)text.charAt(i))-0x61]++;
freq[26]++;
}
}
return freq;
}
public static void output(int[] freq) {
char curLetter;
System.out.println("Total number of letters : " + freq[26] + "\n");
for (int i = 0; i <= 25; i++) {
curLetter = (char)(i + 0x61);
System.out.println(curLetter + " - " +
freq[i]);
}
}
}