Following K&R, i wrote this program as an exercise. It was a while ago when I was still using Windows. Since then I have switched to Linux and the program won't run the way it used, even when simulating cmd in WINE.
#include <stdio.h>
#define IN 1 /* inside a word */
#define OUT 0 /* outside a word */
int main(){
int c, state, nchar;
int i, j; /* for loop variables */
int charcount[15]; /* record word lengths from 1 to 15 characters */
state = IN; /* start inside the word, if whitespace inputted on first key-press ++charcount[-1], which doesn't exist */
nchar = 0; /* initialise word character count variable */
for(i = 0; i<= 14; ++i) /* initialise word length array */
charcount[i] = 0;
printf("Input your text, then type ^Z(Ctrl + Z) on a new line for a word length distribution histogram.\n");
printf("Special characters will be counted as part of a word.\n\n");
while((c = getchar()) != EOF){
if(state == IN){
if(c == ' ' || c == '\n' || c == '\t'){
if ((nchar - 1) <= 14) /* check if character count is above the limit of 15 */
++charcount[nchar-1]; /* increase number of characters of this length, not count the last character inputed(space)*/
else
++charcount[14]; /* if character count > 15, increase 15+ section of the array(charcount[14]) */
state = OUT; /* stop counting character */
}
++nchar; /* increase character count of word if input isn't a whitespace */
}
else if(c != ' ' && c != '\n' && c != '\t'){ /* && not || because the latter is always true, fuuuuck i'm an idiot... */
state = IN; /* go back to recording character count */
nchar = 1; /* count latest character */
}
}
for(i = 0; i< 14; ++i){ /* print histogram by looping through word length record up until 14 word character */
printf("\n%4d:", i+1); /* define histogram section names */
for(j = 0; j < charcount[i]; ++j) /* print bar for each section */
putchar('-');
}
printf("\n%3d+:", 15); /* print bar for words 15+ characters long */
for(j = 0; j < charcount[i]; ++j) /* print bar for 15+ section */
putchar('-');
return 0;
}
The program is supposed to print a histogram of the individual word lengths of an input text. To simulate the EOF character from the book I found out you have to press Control+Z on Windows. When I did that, the for loops at the ran and printed the histogram. When I do the equivalent on Linux(Control+D), the program simply stops. How should I solve this? I am guessing that using EOF as a trigger is a bad idea despite its use in K&R, os how should I change my program to make it reliable?