Shubham Maurya
Shubham Maurya

Reputation: 113

How to switch two characters in a string?

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

Upvotes: 0

Views: 1345

Answers (3)

Ian2thedv
Ian2thedv

Reputation: 2701

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

Upvotes: 0

Syam S
Syam S

Reputation: 8499

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;
    }
}

Upvotes: 1

Jeremy Fisher
Jeremy Fisher

Reputation: 2782

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;
}

Upvotes: 0

Related Questions