title: Word count program - stdin
body: <p>For below <a href="https://users.cs.cf.ac.uk/Dave.Marshall/C/node7.html#SECTION00730000000000000000" rel="nofollow">question</a>,</p>
  
  <blockquote>
    <p>Write a program to read English text to end-of-data (type control-D to indicate end of data at a terminal, see below for detecting it), and print a count of word lengths, i.e. the total number of words of length 1 which occurred, the number of length 2, and so on.</p>
    
    <p>Define a word to be a sequence of alphabetic characters. You should allow for word lengths up to 25 letters.</p>
    
    <p>Typical output should be like this:</p>
  
  <pre><code>        length 1 : 10 occurrences
          length 2 : 19 occurrences
    length 3 : 127 occurrences
       length 4 : 0 occurrences
          length 5 : 18 occurrences
          ....
  </code></pre>
    
    <p>To read characters to end of data see above question.</p>
  </blockquote>
  
  <hr>
  
  <p>Here is my working solution,</p>
  
  <pre><code>#include&lt;stdio.h&gt;
  int main(void){
    char ch;
    short wordCount[20] = {0};
    int count = 0;
    while(ch = getchar(), ch &gt;= 0){
      if(ch == ' ' || ch == ',' || ch == ';'|| ch == ':'|| ch == '.'|| ch == '/'){
        wordCount[count]++;
        count=0;
      }else{
        count++;
      }
    }
    wordCount[count]++; // Incrementing here looks weird to me
  
    for(short i=1; i&lt; sizeof(wordCount)/sizeof(short); i++){
      printf("\nlength %d : %d occurences",i, wordCount[i]);
    }
  }
  </code></pre>
  
  <hr>
  
  <p>Question:</p>
  
  <p>1)</p>
  
  <p>From code elegance aspect, Can I avoid incrementing(++) <code>wordCount</code> outside <code>while</code> loop?</p>
  
  <p>2)</p>
  
  <p>Can I make <code>wordCount</code> array size more dynamic based on word size, rather than constant size <code>20</code>?</p>
  
  <p><em>Note: Learnt about <code>struct</code> but am yet to learn dynamic structures like <code>Linkedlist</code></em></p>