001package algs51; // section 5.0 002import stdlib.*; 003/* *********************************************************************** 004 * Compilation: javac Count.java 005 * Execution: java Count alpha < input.txt 006 * 007 * Create an alphabet specified on the command line, read in a 008 * sequence of characters over that alphabet (ignoring characters 009 * not in the alphabet), computes the frequency of occurrence of 010 * each character, and print out the results. 011 * 012 * % java Count ABCDR < abra.txt 013 * A 5 014 * B 2 015 * C 1 016 * D 1 017 * R 2 018 * 019 * % java Count 0123456789 < pi.txt 020 * 0 99959 021 * 1 99757 022 * 2 100026 023 * 3 100230 024 * 4 100230 025 * 5 100359 026 * 6 99548 027 * 7 99800 028 * 8 99985 029 * 9 100106 030 * 031 *************************************************************************/ 032 033 034 035public class Count { 036 public static void main(String[] args) { 037 Alphabet alpha = new Alphabet(args[0]); 038 int R = alpha.R(); 039 int[] count = new int[R]; 040 String a = StdIn.readAll(); 041 int N = a.length(); 042 for (int i = 0; i < N; i++) 043 if (alpha.contains(a.charAt(i))) 044 count[alpha.toIndex(a.charAt(i))]++; 045 for (int c = 0; c < R; c++) 046 StdOut.println(alpha.toChar(c) + " " + count[c]); 047 } 048} 049