Word length program

Pages: 12
Just take the code that I sent you and replace the comments with what they say to do.
There were some syntax errors in my code as well as some logic errors, but now when refreshed the code I got something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71

/* Warning! Not compiled or tested in any environment */



#include <stdio.h>

#define IN 1
#define OUT 0
#define MAXLENGTH 15


int main()

{

int c, i, j, word, length, overlong_words;

int word_length[MAXLENGTH+1];

  for (i=0; i <= MAXLENGTH; ++i) 
  word_length[i] = 0;            
 

 word = OUT;
 
 while ((c = getchar()) != EOF) {
 	
 if (c == ' ' || c == '\n' || c == '\t')
 {
   if (word == IN)
     {
       if (length <= MAXLENGTH)
        ++word_length[length];
     }
         
    else 
          {   
              ++overlong_words;
              word = OUT;
          }

        length = 0;                              // Now reset word length
    }
          
      else {
              if (word == OUT)
      
               {
                  length = 0;
                  word = IN;
     	
               }
     
       ++length;
 	
      }

  }

   
   printf( "histogram:");
  for (i = 0; i < MAXLENGTH; ++i)     {
 printf( "%2d", i);
 for (j = 0; j < word_length[i]; ++j)
    printf("*");
    printf("\n");
    
   }
}



I think that my logic here is fairly correct, and that`s why I`m a bit confused why it didn`t count the word lengths correctly.

why this isnt workinghistogram: 0
1
2
3*
4**
5
6
7
8
9
10
11
12
13
14

It seems that the program doesn`t count the last word
Last edited on
Topic archived. No new replies allowed.
Pages: 12