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

Swap the corner words and flip the middle characters

王林
Release: 2023-08-21 08:49:06
forward
605 people have browsed it

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

Output

Jvaa wlrod tihs is Hlleo
Copy after login
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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template