-
Notifications
You must be signed in to change notification settings - Fork 2
/
LetterCount.java
38 lines (36 loc) · 1.62 KB
/
LetterCount.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
public class Main {
public static void main(String[] args) {
String[] strings = {
"A STITCH IN TIME SAVES NINE",
"DON'T EAT YELLOW SNOW"
};
int[] letterCount = new int[26];
for (int count=0; count<strings.length; count++){
String current = strings[count];
char[] letters = current.toCharArray();
for (int count2=0;count2<letters.length;count2++){
//find the letter while iterating through the strings array
char lett = letters[count2];
if (lett >= 'A' && lett <= 'Z'){
//for each index from letterCount array
//we add one to it when the letter occurs
//for example if letter is 'A' then we substract 'A'
//in order to add one to letter 'A' which will be index 0
//for 'B' we substract 'A' to go in index 1 and add in that index
letterCount[lett - 'A']++;
}
}
}
for (char count='A';count<='Z';count++){
//for each index in the letterCount
//we print the count of the letters
//for example if letterCount[count] = 'A' (65) then we substract 'A' (65)
//and result will be index 0 which is the letter 'A'
//another example: if letterCount[count] = 'B' (66) the we substract 'A' (65)
//and result will be index 1 which is letter 'B'
System.out.print(count + ": " +
letterCount[count - 'A'] + " ");
}
System.out.println();
}
}