/** * freq.c * A simple frequency count program. * * Copyright (c) 2015 Samuel A. Rebelsky. All rights reserved. * * freq.c is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * freq.c is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with freq.c. If not, see . */ // +----------+------------------------------------------------------ // | Includes | // +----------+ #include #include // +-------+--------------------------------------------------------- // | Utils | // +-------+ /** * Compute an index for a character. */ int indexOf (int ch) { ch = tolower(ch); if (islower(ch)) return ch - 'a'; else if (ch == ' ') return 26; else return 27; } // indexOf // +------+---------------------------------------------------------- // | Main | // +------+ int main () { int counts[28]; // American English int i; int ch; for (i = 0; i < 28; i++) counts[i] = 0; while ((ch = getchar()) != EOF) ++counts[indexOf(ch)]; for (i = 0; i < 13; i++) printf(" %c ", i + 'a'); printf("\n"); for (i = 0; i < 13; i++) printf("%3d ", counts[i]); printf("\n"); for (i = 13; i < 26; i++) printf(" %c ", i + 'a'); printf("\n"); for (i = 13; i < 26; i++) printf("%3d ", counts[i]); printf("\n"); printf("\n"); printf("%3d\n", counts[26]); return 0; } // main()