title: Code can't reverse the string. It prins empty
body: <pre><code>#include &lt;iostream&gt;
  #include &lt;string.h&gt;
  
  using namespace std;
  
  int main()
  {
      string x;
      cout &lt;&lt; &quot;input&quot;;
      cin &gt;&gt; x;
      
      int y = x.size();
      
      string f;
      
      for (int i = 0; i &lt;= y-1; i++){
      
      f[i] = x[y-(1+i)];
      f[y] = '\0';
      
      }
      
      cout &lt;&lt; f &lt;&lt; endl;
      return 0;
      
  }
  </code></pre>
  <p>As the code above shows, I am trying to reverse the string user's input.</p>
  <p><code>cin &gt;&gt; x;</code> stores the string input as <code>x</code>.</p>
  <p><code>int y = x.size();</code> gets the size of the string.</p>
  <p><code>f[i] = x[y-(1+i)];</code> reverses the string input.</p>
  <p>Example:</p>
  <pre><code>Let the input be &quot;hello&quot;,
  So y=5.
  f[0] = x[4] = o;
  ...
  f[3] = x[1] = e;
  f[4] = x[0] = h;
  </code></pre>
  <p><code>cout &lt;&lt; f &lt;&lt; endl;</code> should print <code>f</code>.</p>
  <p>BUT the code does not work.</p>
  <p>I fear that it is because this:</p>
  <pre><code>f[i] = x[y-(1+i)];
  f[y] = '\0';
  </code></pre>
  <p>is inside of keys <code>{}</code> and so does not change the value of <code>f</code>. Is that the problem? How to fix it?</p>