Last active
March 20, 2021 13:57
-
-
Save Learath2/24b42bf6e32568697c1d to your computer and use it in GitHub Desktop.
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
//K&R2 Exercise 1-13 "Histogram of word lengths" | |
#include <stdio.h> | |
#define OUT 0 | |
#define IN 1 | |
#define MAX_WORD_LENGTH 20 | |
main() | |
{ | |
int c = 0, wl = 0, i = 0; | |
int wc[MAX_WORD_LENGTH]; | |
int state = OUT; | |
for(i = 0; i < MAX_WORD_LENGTH; i++) | |
wc[i] = 0; | |
while((c = getchar()) != EOF) { | |
if(c == ' ' || c == '"' || c == '\n') | |
state = OUT; | |
else | |
state = IN; | |
if(state == IN) | |
wl++; | |
else if(state == OUT && wl > 0) { | |
if(wl > MAX_WORD_LENGTH) | |
wl = MAX_WORD_LENGTH - 1; | |
wc[wl]++; | |
wl = 0; | |
} | |
} | |
for(i = 0; i < MAX_WORD_LENGTH; i++){ | |
printf("%3d:", i); | |
while(wc[i] != 0){ | |
putchar('|'); | |
wc[i]--; | |
} | |
putchar('\n'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment