Hi,
I am trying to write a small program that counts the number of times a 1 letter, 2 letter, 3 letter, 4 letter etc. words occur and print the frequency of their occurance as:
1 letter - 1
2 letter - 3
3 letter - 8
...
...
The program below seems to fail to print this. What am I doing wrong? Thank you for the help.
Code:
#include <stdio.h>
int main()
{
int c;
int inspace = 0;
int wordlen = 0;
int maxchar = 0;
int words[] = {0};
int i;
int value = 0;
while((c = getchar()) != EOF) {
if(c == ' ' || c == '\t' || c == '\n') {
if(inspace == 0) {
inspace = 1;
if(wordlen > 0) {
printf("%d\n", wordlen);
++words[wordlen-0];
}
wordlen = 0;
}
}else{
if(inspace == 1) {
inspace = 0;
}
++wordlen;
}
}
for (i = 1; i<= 7; ++i) {
printf("word size = %d Frequency = %d\n", i, words[i]);
}
return 0;
}
After compiling, I am issuing the following command on my Linux machine at the command prompt:
where text_file contains the following text:
Code:
a is it not not perfect perfect eightin
Any help is appreciated. Thanks.
Andrew.