title: Run-Time Check Failure #2 - Stack around the variable 'command' was corrupted
body: <p>I'm getting this error while running the code above: </p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  int ln(char *str);
  int compare(char *str1, char *str2);
  void reverse(char *str, int n);
  
  void main(){
  
  int n;
  int len;
  char *str;
  char command[2];
  
  printf("Enter your string:\n");
  scanf("%d", &amp;n);
  str = malloc(n);
  scanf("%s", str);
  
  printf("&gt;");
  scanf("%s", command);
  
  if (compare(command, "ln")) {
      len = ln(str);
      printf("%d\n", len);
  }
  
  if (compare(command, "rv")) {
      reverse(str, n);
      printf("The current string is %s\n", str);
  }
  
  free(str);
  }
  
  
  int ln(char *str) {
  int i = 0;
  while (str[i] != '\0') {
      i++;
  }
  
  return i;
  }
  
  int compare(char *str1, char *str2) {
  int i = 0;
  while (str1[i] == str2[i] &amp;&amp; str1[i] != '\0' &amp;&amp; str2[i] != '\0') {
      i++;
  }
  
  if (str1[i] == '\0' &amp;&amp; str2[i] == '\0') {
      return 1;
  }
  
  else {
      return 0;
  }
  }
  
  void reverse(char *str, int n){
  int i;
  for (i = 0; i &lt; n / 2; i++){
      char tmp = str[i];
      str[i] = str[n - 1 - i];
      str[n - i - 1] = tmp;
  }
  
  }
  </code></pre>
  
  <p>I've searched online, but any related error I've read about was about writing to much to the allocated memory and such things.
  The error happens when I enter the values "5 abcde" or "4 abcd" or any other number &amp; string (the number equals the string length), and the "rv" (reverse) command.
  While debugging, I found that the code works perfectly, and the error appears only at the last line (after the string has been reversed).</p>
  
  <p>Any help would be appriciated!</p>