/**
 * LetterCounter.java
 * 
 * Computer Science S-111
 * 
 * Reads a line of text from the console and counts the number of letters of
 * each kind that appear in the text.
 */

import java.util.*;

public class LetterCounter {
    /*
     * countLetters - Count the number of each type of letter that appear
     * in the array of characters that is passed in as a parameter 
     * (the array named letters).
     *
     * precondition: the lowerCaseCounts and upperCaseCounts arrays that are
     * passed in as parameters must each be an array of length 26
     * so that we can use them as follows:
     *    lowerCaseCounts[0] will store the number 'a' characters
     *    lowerCaseCounts[1] will store the number 'b' characters
     *    lowerCaseCounts[2] will store the number 'c' characters
     *    etc.
     *    and similarly for upperCaseCounts.
     * 
     * postcondition: the arrays passed in for lowerCaseCounts and 
     * upperCaseCounts will hold the counts.
     */
    public static void countLetters(char[] letters, int[] lowerCaseCounts, int[] upperCaseCounts) {
        int countIndex;
        
        for (int i = 0; i < letters.length; i++) {
            if (letters[i] >= 'a' && letters[i] <= 'z') {
                countIndex = letters[i] - 'a';
                lowerCaseCounts[countIndex]++;
            } else if (letters[i] >= 'A' && letters[i] <= 'Z') {
                countIndex = letters[i] - 'A';
                upperCaseCounts[countIndex]++;
            }
        }
    }

    /*
     * printResults - print the counts stored in the arrays that are 
     * passed in as parameters.  The output looks something like this:
     *   a: 3     A: 0
     *   b: 0     B: 1
     *   etc.
     */
    public static void printResults(int[] lowerCaseCounts, int[] upperCaseCounts) {
        for (int i = 0; i < lowerCaseCounts.length; i++) {
            char lowerLetter = (char)('a' + i);
            char upperLetter = (char)('A' + i);
            System.out.print(lowerLetter + ": " + lowerCaseCounts[i] + "\t");
            System.out.println(upperLetter + ": " + upperCaseCounts[i]);
        }
    }
    
    public static void main(String[] args) {
        Scanner console = new Scanner(System.in);
        System.out.println("Enter a word or sentence: ");
        String line = console.nextLine();
        
        // convert the string containing the input to an array of characters
        char[] letters = line.toCharArray();
        System.out.println("Here is the input as an array:");
        System.out.println(Arrays.toString(letters));
        System.out.println();
                             
        // We create arrays to store the counts, so that we can pass them 
        // into the method that does the counting.
        int[] lowerCounts = new int[26];
        int[] upperCounts = new int[26];
        System.out.println("Here are the counts arrays before calling countLetters:");
        System.out.println("lowerCounts = " + Arrays.toString(lowerCounts));
        System.out.println("upperCounts = " + Arrays.toString(upperCounts));
        System.out.println();
        
        countLetters(letters, lowerCounts, upperCounts);
        System.out.println("Here are the counts arrays after calling countLetters:");
        System.out.println("lowerCounts = " + Arrays.toString(lowerCounts));
        System.out.println("upperCounts = " + Arrays.toString(upperCounts));
        System.out.println();
        
        printResults(lowerCounts, upperCounts);
    }
}