Last active
April 15, 2018 18:49
-
-
Save sqroot3/7fface991c172c135512c1cbd7ccfa40 to your computer and use it in GitHub Desktop.
charcount.c - prints histogram of frequencies of different characters in input (only printing visible ones)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
/* | |
Author: Matias Lago | |
Objective: Print histogram of frequencies of different characters in input (will only print visible ones) | |
*/ | |
#define STARTING_CHARACTER ' ' | |
#define ENDING_CHARACTER '~' | |
int main() | |
{ | |
int c; | |
int characters[(ENDING_CHARACTER - STARTING_CHARACTER) + 1]; | |
for(int i=STARTING_CHARACTER; i <= ENDING_CHARACTER; ++i) | |
characters[i - STARTING_CHARACTER] = 0; | |
while((c = getchar()) != EOF) { | |
++characters[c - STARTING_CHARACTER]; | |
} | |
for(int i=STARTING_CHARACTER; i <= ENDING_CHARACTER; ++i) { | |
printf("%d (%c): ", i, i); | |
for(int j=0; j < characters[i - STARTING_CHARACTER]; ++j) | |
putchar('-'); | |
putchar('\n'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Works well when: