0

Given the above excerpt from a Java code, I need to modify the code such that it could recursively swap pairs of the content of the string variable, "locationAddress". Please note that the variable, "locationAddress", contains a string of characters, say, abcdefghij. I wish to swap "abcdefghij" in pairs such that the result will be "badcfehgji".

Please kindly assist with the necessary modification to the above Java code excerpt to make it recursively swap pairs of characters in the string variable, "locationAddress".

public void format(DataObject dataSource) throws Exception {
    String locationAddress = dataSource.getValueAsString("Location-Address").substring(4);
    if (dataSource.parameterExists("Location-Address")) {
        dataSource.setParameter("Parameter-Type","400");
        dataSource.setParameter("Parameter-Value", locationAddress);
    }    
}
1
  • 1
    You should try yourself, then post your code here and guys will help you Commented Nov 1, 2018 at 10:09

3 Answers 3

3

Here is a very simple way to do this using regex replacement in Java:

String input = "abcdefghij";
input = input.replaceAll("(.)(.)", "$2$1");
System.out.println(input);

badcfehgji

The idea is to walk down the string, starting at the beginning, capturing two characters at a time, in two different capture groups. Then, just swap those two captured characters in the replacement.

Sign up to request clarification or add additional context in comments.

2 Comments

That's a nice trick! The idea of using a regex never crossed my mind! I was thinking more along the lines of StringBuilder.
@Sweeper ... which is probably the direction you'd go in if you really needed this to run lightning fast, or as fast as possible. Regex has a big overhead. But, for a one off need, my answer is suitable.
1

Here's one solution with StringBuilder:

public static String swapAdjacentPairs(String s) {
    StringBuilder sb = new StringBuilder(s);
    // divide 2 and then multiply by 2 to handle cases where the string length is odd
    // we always want an even string length
    // also note the i += 2
    for (int i = 0 ; i < (s.length() / 2 * 2) ; i += 2) {
        swapAdjacent(sb, i);
    }
    return sb.toString();
}

private static void swapAdjacent(StringBuilder sb, int index) {
    char x = sb.charAt(index);
    sb.setCharAt(index, sb.charAt(index + 1));
    sb.setCharAt(index + 1, x);
}

Usage:

System.out.println(swapAdjacentPairs("abcdefghi"));

Comments

0

A solution using Stream:

String input = "abcdefghijk";
String swapped = IntStream.range(0, input.length())
        .map(i -> i % 2 == 0 ? i == input.length() - 1 ? i : i + 1 : i - 1)
        .mapToObj(input::charAt)
        .map(String::valueOf)
        .collect(Collectors.joining());
System.out.println(swapped); // badcfehgjik

The swapping is driven by the index i. If i is even and there is a next (i+1) character then it's used. If i is odd then the previous (i-1) character is used.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.