title: Reverse String function in C without library functions
body: <p>This is my task:</p>
  
  <p>Realize function that reverses null terminated string. The prototype of the function is <code>void Reverse(char *ptr);</code>. Do not use standard library functions.</p>
  
  <p>This is my code for now:</p>
  
  <pre><code>void Reverse(char *ptr) {
      char *newString;
      char ch = *ptr;
      unsigned int size = 0;
  
      for (int i = 1; ch != '\0'; i++) {
          ch = *(ptr + i);
          size++;
      }
  
      newString = (char*)malloc(size);
  
      for (int left = 0, right = size - 1; left &lt; size; left++, right--) {
          *(newString + left) = *(ptr + right);
      }
  
      printf("%s", newString);
      printf("\n");
  }
  </code></pre>
  
  <p>It reverses the string and saves it in the newString</p>
  
  <p>My first problem is that when I print the newString to see if the functions works the string is reversed, but after it there are some symbols.</p>
  
  <p>For example:
  If I have <code>char *str = "hello";</code> and <code>Reverse(str);</code> in the <code>main</code> method
  the result of <code>printf("%s", newString)</code> will be  <code>olleh****</code>.</p>
  
  <p>But if change the
  <code>newString = (char*)malloc(size);</code>   to
  <code>newString = (char*)malloc(1);</code>      it works fine.</p>
  
  <p>My second problem is that I don't know how to save the newString into the given one. I am using a new String because the given one can't be changed.</p>