Skip to content

Instantly share code, notes, and snippets.

@vayn
Created May 9, 2016 07:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vayn/f3915bbfa634865f82d33f046965afc1 to your computer and use it in GitHub Desktop.
Save vayn/f3915bbfa634865f82d33f046965afc1 to your computer and use it in GitHub Desktop.
Analyse length of words and draw the statistics with a chart
#include <stdio.h>
#define IN 1
#define OUT 0
#define MAXWORDLEN 10
void drawChart(int maxValue, int *wordsLengthNum)
{
int i, j;
for (i = maxValue; i > 0; i--) {
printf("%4d | ", i);
for (j = 1; j <= MAXWORDLEN; j++) {
if (wordsLengthNum[j] >= i) {
printf("* ");
} else {
printf(" ");
}
}
printf("\n");
}
printf(" +");
for(i = 0; i <= MAXWORDLEN; i++) {
printf("---");
}
printf("\n ");
for (i = 0; i < MAXWORDLEN; i++) {
printf("%2d ", i + 1);
}
printf(">%d\n", MAXWORDLEN);
}
int main(int argc, char** argv)
{
int c, i, nl, nw, nc, state;
int maxLength, wordLength;
int wordsLengthNum[MAXWORDLEN+1] = {0};
state = OUT;
nl = nw = nc = 0;
wordLength = maxLength = 0;
while ((c = getchar()) != EOF) {
++nc;
if (c == '\n') {
++nl;
}
if (c == ' ' || c == '\n' || c == '\t') {
if (state == IN) {
++wordsLengthNum[wordLength];
if (wordLength > maxLength) {
maxLength = wordLength;
}
wordLength = 0;
}
state = OUT;
}
else if (state == OUT) {
state = IN;
++nw;
}
if (state == IN) {
if (wordLength <= MAXWORDLEN) {
++wordLength;
} else {
wordLength = 0;
}
}
}
printf("%d %d %d\n\n", nl, nw, nc);
drawChart(maxLength, wordsLengthNum);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment