• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Creating Histogram on Character Occurrences
#1
The following code will count how many times a character appears in entered input. And create a histogram to detail it. I thought it was some pretty cool code, so I decided to share.

[code=c]#include <stdio.h>
#define EOL '\n'
#define ARYLEN 256 /* Length of Ascii Alphabet.. From 0 */

main(){
int c, i, x;
int length[ARYLEN];

for(x = 0; x < ARYLEN;x++)
length[x] = 0;

while( (c = getchar() ) != EOL){
length[c]++;
if (c == EOL)
break;
}


for(x = 0; x < ARYLEN; x++){
if( length[x] > 0){
printf("%c | ", x);
for(i = 1; i <= length[x]; ++i){
printf("*");
}
printf("\n");
}
}
}[/code]
[Image: nomnomnom.jpg]
;7$=v?%v%#5>v7v8994
The decrypt code is V, I could not make it any simpler!
  Reply


Messages In This Thread
Creating Histogram on Character Occurrences - by Drumm - 11-01-2011, 09:57 AM

Forum Jump: