forked from AllenDowney/ThinkJavaCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEx2.java
More file actions
31 lines (25 loc) · 987 Bytes
/
Copy pathEx2.java
File metadata and controls
31 lines (25 loc) · 987 Bytes
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
import java.util.Arrays;
public class Ex2 {
public static void main(String[] args) {
String myString = "I'm just a string, yes I'm only a string";
letterHist(myString);
}
public static int[] letterHist(String textString) {
String upperCaseString = textString.toUpperCase();
String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int[] histogram = createEmptyArray(alphabet.length());
for (int i = 0; i < upperCaseString.length(); i++) {
char stringCharacter = upperCaseString.charAt(i);
int whichLetterOfTheAlphabet = alphabet.indexOf(stringCharacter);
if (whichLetterOfTheAlphabet >= 0) {
histogram[whichLetterOfTheAlphabet] += 1;
}
}
// System.out.println(Arrays.toString(histogram));
return histogram;
}
public static int[] createEmptyArray(int size) {
int[] emptyArray = new int[size];
return emptyArray;
}
}