Home  >  Article  >  Java  >  Swap the corner words and flip the middle characters

Swap the corner words and flip the middle characters

王林
王林forward
2023-08-21 08:49:06545browse

Swap the corner words and flip the 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 wealth of string manipulation tools. From basic concatenation and comparison operations to more complex tasks like string reversal and swapping, Java's String API can handle it all. An interesting problem is to swap the first and last words of a string and reverse the middle characters. This problem can be solved by combining Java's built-in String methods with 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, keeping the first and last characters of the string unchanged.

method

The strategy to solve this problem is simple −

  • 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.

  • Recombine words into strings.

Example

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

Output

Jvaa wlrod tihs is Hlleo
The Chinese translation of

Explanation

is:

Explanation

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 rejoin the words 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.

Conclusion

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.

The above is the detailed content of Swap the corner words and flip the middle characters. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete