Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 257989
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T22:08:35+00:00 2026-05-11T22:08:35+00:00

I am practicing over the summer to try and get better and I am

  • 0

I am practicing over the summer to try and get better and I am a little stuck on the following:

http://www.javabat.com/prob/p123384

Given a string, return a new string where the first and last chars have been exchanged.


Examples:

frontBack("code") → "eodc"
frontBack("a") → "a"
frontBack("ab") → "ba"

Code:

public String frontBack(String str) 
{
    String aString = "";
    
    if (str.length() == 0){
        return "";
    }
    
    char beginning = str.charAt(0);
    char end = str.charAt(str.length() - 1);
    
    str.replace(beginning, end);
    str.replace(end, beginning);
    
    return str;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T22:08:36+00:00Added an answer on May 11, 2026 at 10:08 pm

    Rather than using the String.replace method, I’d suggest using the String.substring method to get the characters excluding the first and last letter, then concatenating the beginning and end characters.

    Furthermore, the String.replace method will replace all occurrences of the specified character, and returns a new String with the said replacements. Since the return is not picked up in the code above, the String.replace calls really don’t do much here.

    This is because String in Java is immutable, therefore, the replace method cannot make any changes to the original String, which is the str variable in this case.


    Also to add, this approach won’t work well with Strings that have a length of 1. Using the approach above, a call to String.substring with the source String having a length of 1 will cause a StringIndexOutOfBoundsException, so that will also have to be taken care of as a special case, if the above approach is taken.

    Frankly, the approach presented in indyK1ng’s answer, where the char[] is obtained from the String and performing a simple swap of the beginning and end characters, then making a String from the modified char[] is starting to sound much more pleasant.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been practicing my C++, as it's gotten a little rusty since college, and
I am practicing how to find and remove dead code. I have the following
I was practicing writing macros and I can't seem to get defn to work.
I am practicing with jQuery and trying to write a little interaction where a
Practicing what is written here: ScalaForms , I have created the following form: val
I am practicing some bool functions and I seem to be stuck any help
I am practicing using GUIs. I have been following a set of instructions however
Started practicing with XML and C# and I have an error message of There
While practicing the jxl API, i cant extract details from certain Excel sheet its
I am practicing at getting consistent with my error handling, and I keep hoping

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.