首页 > Java > java教程 > 正文

交换角落的单词并翻转中间的字符

王林
发布: 2023-08-21 08:49:06
转载
651人浏览过

交换角落的单词并翻转中间的字符

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提供了丰富的字符串操作工具。从基本的拼接和比较操作到更复杂的任务,如字符串反转和交换,Java的String API都可以处理。一个有趣的问题是交换字符串的首尾单词并反转中间字符。这个问题可以通过结合Java的内置String方法和一些手动逻辑来解决。

Problem Statement

给定一个字符串,我们需要交换第一个和最后一个单词,并且反转中间字符的顺序,保持字符串的第一个和最后一个字符不变。

方法

解决这个问题的策略很简单 −

  • Split the input string into words.

  • 交换第一个和最后一个单词。

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

  • 将单词重新组合成字符串。

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
登录后复制

Explanation

的中文翻译为:

解释

让我们用字符串"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"].

最后,我们将单词重新拼接成字符串:"Jvaa wlrod tihs is Hlleo"。

所以,swapAndReverse("Hello world this is Java")的输出是"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.

以上就是交换角落的单词并翻转中间的字符的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:tutorialspoint网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号