title: How can I exchange the first and last characters of a string in Java?
body: <p>I am practicing over the summer to try and get better and I am a little stuck on the following:</p>
  <p><a href="http://www.javabat.com/prob/p123384" rel="noreferrer">http://www.javabat.com/prob/p123384</a></p>
  <blockquote>
  <p>Given a string, return a new string where the first and last chars have been exchanged.</p>
  </blockquote>
  <hr />
  <h3>Examples:</h3>
  <pre><code>frontBack(&quot;code&quot;) → &quot;eodc&quot;
  frontBack(&quot;a&quot;) → &quot;a&quot;
  frontBack(&quot;ab&quot;) → &quot;ba&quot;
  </code></pre>
  <hr />
  <h3>Code:</h3>
  <pre><code>public String frontBack(String str) 
  {
      String aString = &quot;&quot;;
      
      if (str.length() == 0){
          return &quot;&quot;;
      }
      
      char beginning = str.charAt(0);
      char end = str.charAt(str.length() - 1);
      
      str.replace(beginning, end);
      str.replace(end, beginning);
      
      return str;
  }
  </code></pre>