{"content":{"id":357,"title":"K\u0026R Solutions - 1.14","body":"\u003cp\u003e\u003cb\u003eQ:\u003c/b\u003e Write a program to print a histogram of the frequencies of different characters\r\nin its input.\u003c/p\u003e\r\n\u003cp\u003e\u003cb\u003eA:\u003c/b\u003e\u003c/p\u003e \r\n\u003cpre\u003e\u003ccode class=\"cpp\"\u003e#include \u0026ltstdio.h\u0026gt\r\n\r\nint main(void) \r\n{\r\n int FREQUENCY_LENGTH = 256; // We'll just count all ASCII values. Why not?\r\n int frequencies[FREQUENCY_LENGTH];\r\n int i, j, c;\r\n int tally = 0;\r\n\r\n for (i = 0; i \u003c FREQUENCY_LENGTH; ++i) {\r\n frequencies[i] = 0;\r\n }\r\n\r\n // This part gets a lot more simple.\r\n while ((c = getchar()) != EOF) {\r\n ++frequencies[c];\r\n }\r\n\r\n printf(\"\\n\");\r\n printf(\"Character Frequencies\\n\");\r\n printf(\"---------------------\\n\");\r\n /* For the sake of brevity, we will only print visible characters.\r\n * The ASCII values for visible characters are all between 33 and 126,\r\n * a fact I pulled off www.asciitable.com\r\n */\r\n for (i = 33; i \u003c= 126; ++i) {\r\n printf(\"%c | \", i);\r\n\r\n for (j = 0; j \u003c frequencies[i]; ++j) {\r\n printf(\"-\");\r\n }\r\n\r\n printf(\"\\n\");\r\n }\r\n\r\n return 0;\r\n}\r\n\u003c/code\u003e\u003c/pre\u003e\r\n\u003cpre\u003e\u003ccode class=\"lisp\"\u003eAmerica! Land of my dreams, home of the Whopper!\r\n\r\n(ctrl+d)\r\n\r\nCharacter Frequencies\r\n---------------------\r\n! | --\r\n\" | \r\n# | \r\n$ | \r\n% | \r\n\u0026 | \r\n' | \r\n( | \r\n) | \r\n* | \r\n+ | \r\n, | -\r\n- | \r\n. | \r\n/ | \r\n0 | \r\n1 | \r\n2 | \r\n3 | \r\n4 | \r\n5 | \r\n6 | \r\n7 | \r\n8 | \r\n9 | \r\n: | \r\n; | \r\n\u003c | \r\n= | \r\n\u003e | \r\n? | \r\n@ | \r\nA | -\r\nB | \r\nC | \r\nD | \r\nE | \r\nF | \r\nG | \r\nH | \r\nI | \r\nJ | \r\nK | \r\nL | -\r\nM | \r\nN | \r\nO | \r\nP | \r\nQ | \r\nR | \r\nS | \r\nT | \r\nU | \r\nV | \r\nW | -\r\nX | \r\nY | \r\nZ | \r\n[ | \r\n\\ | \r\n] | \r\n^ | \r\n_ | \r\n` | \r\na | ---\r\nb | \r\nc | -\r\nd | --\r\ne | -----\r\nf | --\r\ng | \r\nh | ---\r\ni | -\r\nj | \r\nk | \r\nl | \r\nm | ----\r\nn | -\r\no | ----\r\np | --\r\nq | \r\nr | ---\r\ns | -\r\nt | -\r\nu | \r\nv | \r\nw | \r\nx | \r\ny | -\r\nz | \r\n{ | \r\n| | \r\n} | \r\n~ | \r\n\u003c/code\u003e\u003c/pre\u003e","publication_date":"2018-11-06T01:00:00.000Z","created_at":"2018-11-05T23:37:34.000Z","updated_at":"2018-11-06T22:40:55.000Z","user_id":1,"rating":null},"tags":"\u003ca class=\"changeable-title\" href=\"/q?tag=k%26r\"\u003ek\u0026amp;r\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=programming\"\u003eprogramming\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=answers\"\u003eanswers\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=c\"\u003ec\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=unix\"\u003eunix\u003c/a\u003e"}

K&R Solutions - 1.14

Q: Write a program to print a histogram of the frequencies of different characters in its input.

A:

#include <stdio.h>

int main(void) 
{
  int FREQUENCY_LENGTH = 256; // We'll just count all ASCII values. Why not?
  int frequencies[FREQUENCY_LENGTH];
  int i, j, c;
  int tally = 0;

  for (i = 0; i < FREQUENCY_LENGTH; ++i) {
    frequencies[i] = 0;
  }

  // This part gets a lot more simple.
  while ((c = getchar()) != EOF) {
    ++frequencies[c];
  }

  printf("\n");
  printf("Character Frequencies\n");
  printf("---------------------\n");
  /* For the sake of brevity, we will only print visible characters.
   * The ASCII values for visible characters are all between 33 and 126,
   * a fact I pulled off www.asciitable.com
   */
  for (i = 33; i <= 126; ++i) {
    printf("%c | ", i);

    for (j = 0; j < frequencies[i]; ++j) {
      printf("-");
    }

    printf("\n");
  }

  return 0;
}
America! Land of my dreams, home of the Whopper!

(ctrl+d)

Character Frequencies
---------------------
! | --
" | 
# | 
$ | 
% | 
& | 
' | 
( | 
) | 
* | 
+ | 
, | -
- | 
. | 
/ | 
0 | 
1 | 
2 | 
3 | 
4 | 
5 | 
6 | 
7 | 
8 | 
9 | 
: | 
; | 
< | 
= | 
> | 
? | 
@ | 
A | -
B | 
C | 
D | 
E | 
F | 
G | 
H | 
I | 
J | 
K | 
L | -
M | 
N | 
O | 
P | 
Q | 
R | 
S | 
T | 
U | 
V | 
W | -
X | 
Y | 
Z | 
[ | 
\ | 
] | 
^ | 
_ | 
` | 
a | ---
b | 
c | -
d | --
e | -----
f | --
g | 
h | ---
i | -
j | 
k | 
l | 
m | ----
n | -
o | ----
p | --
q | 
r | ---
s | -
t | -
u | 
v | 
w | 
x | 
y | -
z | 
{ | 
| | 
} | 
~ |