0

Given a string as input, return the string with its last 2 chars swapped. And, if the string has less than 2 chars, do nothing and return the input string.

Here is the code I wrote so far:

public class SwapLastChars {

    static String testcase1 = "Hello";

    public static void main(String args[]) {
        SwapLastChars testInstance = new SwapLastChars();
        String result = testInstance.swap(testcase1);
        System.out.println(result);
    }

    public String swap(String str1) {

        String str = "";
        int length = str1.length();
        char last = str1.charAt(length - 1);
        char l = str1.charAt(length - 2);
        if (length == 1)
            return str1;
        for (int i = 0; i < str1.length() - 2; i++) {
            str = str + str1.charAt(i);
        }
        str = str + last + l;
        return str;
    }
}

Problem is in my test cases,any help?

Testcase    Pass/Fail   Parameters  Actual Output   Expected Output

1           pass        'aabbccdd'      aabbccdd        aabbccdd

2           fail        'A'             null            A

3           pass        'hello'         helol           helol
perror
  • 7,071
  • 16
  • 58
  • 85
  • There is no way that code can return null when invoked with 'A'. Can you show us your Testcase code, as that one might be faulty – Fermin Silva Aug 11 '15 at 13:18
  • 1
    could you also change the formatting of your test cases for readability please – Jeremy Fisher Aug 11 '15 at 13:19
  • Are you sure this is teh code that runs? The behavior does not match the code. Please add a main() to your sample code and run it yourself. –  Aug 11 '15 at 13:19
  • Your test is faulty. Your swap() method doesn't return `null` for 'A', it throws a StringIndexOutOfBoundsException. – Erick G. Hagstrom Aug 11 '15 at 13:31

3 Answers3

1

If you pass "A" you'll get StringIndexOutOfBoundsException rather than null. Unless you suppress it in a catch clause and return null.

Quick fix. Move the length check to start of the method. That should solve your issue.

public class SwapLastChars {

    static String testcase1 = "A";

    public static void main(String args[]) {
        SwapLastChars testInstance = new SwapLastChars();
        String result = testInstance.swap(testcase1);
        System.out.println(result);
    }

    public String swap(String str1) {
        if(str1 == null || str1.length() < 2) { //Move here
            return str1;
        }
        String str = "";
        int length = str1.length();
        char last = str1.charAt(length - 1);
        char l = str1.charAt(length - 2);
        for(int i = 0; i < str1.length() - 2; i++) {
            str = str + str1.charAt(i);
        }
        str = str + last + l;
        return str;
    }
}
Syam S
  • 8,421
  • 1
  • 26
  • 36
0

You should check for length at the very beginning of your function.

public String swap(String str1){

    String str="";
    int length=str1.length();
    if (length <=2)
       return str1;
    char last=str1.charAt(length-1);
    char l=str1.charAt(length-2);
    for(int i=0;i<str1.length()-2;i++)
     {
        str=str+str1.charAt(i);
     }
    str=str+last+l;
    return str;
}
Jeremy Fisher
  • 2,510
  • 7
  • 30
  • 59
0

I know this has already been answered, but I feel OPs swap method can be simplified by using a StringBuilder:

public static String swap(String word) {
    //Answer by Syam
    if (word == null || word.length() < 2) {
        return word;
    }

    //Create new StringBuilder
    StringBuilder s = new StringBuilder(word);

    //Get second last char
    char c = s.charAt(s.length() - 2);

    //Replace second last char with last char
    s.setCharAt(s.length() - 2, s.charAt(s.length() - 1));
    //replace last char with stored char
    s.setCharAt(s.length() - 1, c);

    return s.toString();
}

Run:

System.out.println(swap("aabbccdd"));
System.out.println(swap("A"));
System.out.println(swap("hello"));

Output:

aabbccdd
A
helol

And here is why

Community
  • 1
  • 1
Ian2thedv
  • 2,691
  • 2
  • 26
  • 47