Swap Corner Words and Reverse Middle Characters

In this article, we'll delve into a fascinating string manipulation problem that involves swapping corner words of a string and reversing the middle characters. This kind of problem is quite common in coding interviews, and it's a great way to enhance your understanding of string manipulation in Java.

Java provides a rich set of tools for string manipulation. From basic operations such as concatenation and comparison to more complex tasks such as string reversing and swapping, Java's String API can handle it all. One intriguing problem is to swap the corner words of a string and reverse the middle characters. This problem can be solved using a combination of Java's inbuilt String methods and some manual logic.

Problem Statement

Given a string, we need to swap the first and last words and reverse the order of the middle characters, leaving the first and last characters of the string intact.


The strategy to solve this problem is straightforward −

  • Split the input string into words.

  • Swap the first and last words.

  • Reverse the order of the middle characters while keeping the first and last characters of the string in their original positions.

  • Join the words back into a string.


Below is a Java function that implements the approach described above −

import java.util.*;

public class Main {
   public static String swapAndReverse(String input) {
      String[] words = input.split(" ");
      // Swap the first and last words
      String temp = words[0];
      words[0] = words[words.length - 1];
      words[words.length - 1] = temp;
      // Reverse the middle characters of the string, leaving the first and last characters intact
      for(int i = 0; i < words.length; i++) {
         if(words[i].length() > 2) {
               String middleCharacters = words[i].substring(1, words[i].length() - 1);
               String reversedMiddleCharacters = new StringBuilder(middleCharacters).reverse().toString();
               words[i] = words[i].charAt(0) + reversedMiddleCharacters + words[i].charAt(words[i].length() - 1);
      // Join the words back into a string
      return String.join(" ", words);
   public static void main(String[] args) {
      System.out.println(swapAndReverse("Hello world this is Java"));


Jvaa wlrod tihs is Hlleo


Let's test our function with the string "Hello world this is Java".

The words in the string are ["Hello", "world", "this", "is", "Java"]. After swapping the first and last words, we get ["Java", "world", "this", "is", "Hello"].

Then we reverse the middle characters of each word, excluding the first and last characters, resulting in ["Jvaa", "wlrod", "tihs", "is", "Hlleo"].

Finally, we join the words back together into a string: "Jvaa wlrod tihs is Hlleo".

So, the output of swapAndReverse("Hello world this is Java") is "Jvaa wlrod tihs is Hlleo".

The swapAndReverse function is working correctly, and it's evident that it's accurately swapping the corner words and reversing the middle characters in the given string. We hope this example clarifies the operation of the function.


Java offers a wide variety of tools for manipulating strings, making it ideal for solving problems like swapping corner words and reversing middle characters in a string. Mastering these skills will serve you well in both coding interviews and your everyday programming tasks.

Updated on: 15-May-2023


Kickstart Your Career

Get certified by completing the course

Get Started