title: Simple word comparison in C
body: <p>I've just written a fairly simple program in C to find <code>smallest word</code> and <code>largest word</code>. I have 3 variables as follow:</p>
  
  <pre><code>char smallest[20]; // smallest word
  char largest[20]; // largest word
  char word[20]; // the word entered by user
  </code></pre>
  
  <p>Then I show a prompt to user to enter any word he want:</p>
  
  <pre><code>while (1) {
    printf("Enter a word: ");
    while((ch = getchar()) != '\n') {
      word[i++] = ch;
    }
  }
  </code></pre>
  
  <p>Then I will compare <code>word</code> with both <code>smallest</code> and <code>largest</code> in order to achieve the result which is to find smallest and largest word among user entered words.</p>
  
  <pre><code>if (strcmp(word, smallest) &lt; 0) {
    strcpy(smallest, word);
  }
  
  if (strcmp(word, largest) &gt; 0) {
    strcpy(largest, word);
  }
  </code></pre>
  
  <p>And finally the program will terminate if user just enter without typing any word.
  The program find <code>largest</code> word correctly but my problem is with finding <code>smallest</code> word.</p>
  
  <p>Here is the entire program if you want copy/paste to test it.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  
  int main(void) {
  
    char smallest[20] = "";
    char largest[20] = "";
    char word[20] = "";
  
    while (1) {
  
        int i = 0;
        char ch;
  
        strcpy(word, "");
  
        printf("Enter a word: ");
        while((ch = getchar()) != '\n') {
          word[i++] = ch;
        }
  
        if (strcmp(word, smallest) &lt; 0) {
          strcpy(smallest, word);
        }
  
        if (strcmp(word, largest) &gt; 0) {
          strcpy(largest, word);
        }
  
  
      if (strlen(word) == 0) break;
    }
  
    printf("Smallest word: %s\n", smallest);
    printf("Largest word: %s\n", largest);
  
    return 0;
  
  }
  </code></pre>
  
  <p>Would you please tell me how to modify it to find both <code>smallest</code> and <code>largest</code> word correctly.</p>