tags:

views:

1397

answers:

12

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;
}
+7  A: 

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.

coobird
i tried this by creating a new String and assigning the valueString newString = str.substring(1, str.length() -2);return end + newString + beginning;but it didnt work
didnt work = wouldn't compile saying illegal start of expression <sorry for the previous unclear answer i gave>
I tried out what I've written and was able to get the compile the code and make it run correctly. "Illegal start of expression" sounds like a compiler error where some token (such as a semicolon) was added or missing.
coobird
+9  A: 

Strings can be split into an array of chars and can be made with an array of chars. For more details on String objects, go to the Java API and click on String in the lower left pane. That pane is sorted alphabetically.

Edit: Since some people are being more thorough, I think I'll give additional details. Create a char array using String's .toCharArray() method. Take the first element and store it in a char, swap the first element with the last, and place the element you stored in a char into the last element into the array, then say:

String temp = new String(charArray);

and return that. This is assuming that charArray is your array of chars.

indyK1ng
+1, this is a good approach as well. The String.toCharArray method can be used to return a char[].
coobird
I think that going over a char array may be overkill for this problem. Why not just do some substrings?
Thorbjørn Ravn Andersen
I'm not looping over an array, I'm just converting to one, switching the two which need to be switched and converting it back into a String. It uses one fewer method call than the substring technique at its fewest method calls.
indyK1ng
+2  A: 

A couple of hints:

  • Strings are immutable, meaning they cannot be changed. Hence, str.replace() does not change str, instead it returns a new string.

  • Maybe replace isn't the best... Consider frontBack("abcabc"): your function, if it were corrected, would replace 'a' with 'c' yielding "cbccbc", then 'c' with 'a' yielding "abaaba". That's not quite right!

John Kugelman
+4  A: 

String instances in Java are immutable. This means that you cannot change the characters in a String; a different sequence of characters requires a new object. So, when you use the replace method, throw away the original string, and use the result of the method instead.

For this method, however, you probably want to convert the String instance to an array of characters (char[]), which are mutable. After swapping the desired characters, create a new String instance with that array.

erickson
+2  A: 

The replace method in String actually returns a String, so if you were to insist on using replace, you'd do:

beginReplace = str.replace( beginning, end );
endReplace = beginReplace.replace( end, beginning );
return( str );

But this actually doesn't solve your specific problem, because replace replaces all occurences of a character in the string with its replacement.

For example, if my string was "apple" and I said "apple".replace( 'p', 'q' ), the resulting string would be "aqqle."

FreeMemory
A: 
public String frontBack(String input)
{
    return
        input.substring(input.length() - 1) +  // The last character
        input.substring(1, input.length() - 1) +  // plus the middle part
        input.substring(0, 1);                  // plus the first character.
}
Daniel Brückner
Man, don't just give him the answer. He's trying to learn.
Welbog
This isn't valid Java code.
erickson
I have never ever written a single line of Java ... was just meant as pseudocode.
Daniel Brückner
Make it look like Pseudo-code then!
oxbow_lakes
+2  A: 

Yet another example without creating additional objects:

if (str.length() > 1) {
    char[] chars = str.toCharArray();
    // replace with swap()
    char first = chars[0];
    chars[0] = chars[chars.length - 1];
    chars[chars.length - 1] = first;
    str = new String(chars);
}

return str;

Edit: Performing the swap on length = 1 string is no-op.

Edit 2: dfa's change to copyValueOf did not make any sense as the Java source says in String.java: "// All public String constructors now copy the data." and the call is just delegated to a string constructor.

kd304
additional object: String.toCharArray() creates "a newly allocated character array" which still is an object.
Carlos Heuberger
True. An user called dfa added that sentence to my answer and made other changes (see edit2). He should have just post his own answer
kd304
A: 

You can use a StringBuilder that represents "a mutable sequence of characters".
It has all methods needed to solve the problem: charAt, setCharAt, length and toString.

Carlos Heuberger
A: 
if (s.length < 2) {
   return s;
}
return s.subString(s.length - 1) + s.subString(1, s.length - 2) + s.subString(0, 1);

(untested, indexes may be of by one...

Thorbjørn Ravn Andersen
+1  A: 

Just another, slightly different, approach, so you get a sense of the spectrum of possibilities. I commend your attention to the quick exit for short strings (instead of nesting the more-complicated processing in an if() clause), and to the use of String.format(), because it's a handy technique to have in your toolbox, not because it's notably better than regular "+" concatenation in this particular example.

public static String exchange(String s) {
    int n = s.length();
    if (n < 2)
     return s;
    return String.format("%s%s%s", s.charAt(n - 1), s.substring(1, n - 1), s.charAt(0));
}
Carl Manaster
+2  A: 

You could use a regex..

return str.replaceFirst("(.)(.*)(.)", "$3$2$1");
rich
Might not work on edge cases where string length < 2
subtenante
+1  A: 

Simple solution is:

public String frontBack(String str) {
  if (str == null || str.length() == 0) {
    return str;
  }
  char[] cs = str.toCharArray();
  char first = cs[0];
  cs[0] = cs[cs.length -1];
  cs[cs.length -1] = first;
  return new String(cs);
}

Using a character array (watch out for the nasty empty String or null String argument!)


Another solution uses StringBuilder (which is usually used to do String manupilation since String itself is immutable.

public String frontBack(String str) {
  if (str == null || str.length() == 0) {
    return str;
  }
  StringBuilder sb = new StringBuilder(str);  
  char first = sb.charAt(0);
  sb.setCharAt(0, sb.charAt(sb.length()-1));
  sb.setCharAt(sb.length()-1, first);
  return sb.toString();
}


Yet another approach (more for instruction than actual use) is this one:

public String frontBack(String str) {
  if (str == null || str.length() < 2) {
    return str;
  }
  StringBuilder sb = new StringBuilder(str);
  String sub = sb.substring(1, sb.length() -1);
  return sb.reverse().replace(1, sb.length() -1, sub).toString();
}

Here the complete string is reversed and then the part that should not be reversed is replaced with the substring. ;)

Maarten Winkels