title: Reversing a string in C does not output the reversed line
body: <p>I'm trying to reverse a string in C. The reverse function simply assigns the character at a given location (in a for loop) to a temp object. I cannot see any logic errors within the program, and the program compile successfully under gcc 4.7.2 with this command:</p>
  
  <pre><code>gcc -Wall -std=c99 reverse.c
  </code></pre>
  
  <p>To recreate the problem: </p>
  
  <p>1.) Run the program and enter a string into your shell</p>
  
  <p>2.) Once finished inputting, press enter/and or your EOF signal. </p>
  
  <p>The problem is that neither the original string is printed, or the reversed string. This is also an exercise from K&amp;R second edition, if you have completed this exercise, a different solution to mine would be appreciated. </p>
  
  <p>I think the bug is caused by the absence of a null character, the famous printf requires a null terminated string to print input to cin. The getline function assigns a null character to the end of the array, surely the null character will be the first character in the string thereto ending the printf (and thus no character/literal is printed). </p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  #define MAXLINE 1000
  
  int geline(char s[], int lim);
  void reverse(char line[],  int length);
  
  int main() 
  {
      char s[MAXLINE];
      char t[MAXLINE];
      int k, len;
  
      while ((len = getline(s, MAXLINE)) &gt; 0) {
          if (len &gt; 1) 
              reverse(s, len);
      }
      printf("%s", s);
      return 0;
  }
  
  void reverse (char input[], int length) 
  {
      char temp[MAXLINE];
      int j = length;
      for (int i = 0; i &lt; length; ++i, --j) {
  
              temp[i] = input[i];
              input[i] = input[j];
              input[j] = temp;
      }
  
  }
  
  
  
  int getline(char s[], int lim)
  {
      int c, i;
  
      for (i=0; (c=getchar()) != EOF &amp;&amp; c!='\n'; ++i) 
          s[i] = c;
      if (c== '\n') {
          s[i] = c;
          ++i;
      }
      s[i] = '\0';
      return i;
  }
  </code></pre>