package ppt10lang包; import java.util.Scanner; //编写一个程序,将下面的一段文本中的各个单词的字母顺序翻转 public class Lianxi2 { public static void main(String[] args) { System.out.println("请输入一段字符串"); Scanner input = new Scanner(System.in); String str1 = input.nextLine(); // 将str1转换为字符串数组str2[] String[] str2 = str1.split(" "); for (int i = 0; i < str2.length; i++) { System.out.print(reverse1(str2[i])+" "); } } //字符串反转 public static String reverse1(String s) { int length = s.length(); if (length <= 1) return s; String left = s.substring(0, length / 2); String right = s.substring(length / 2, length); return reverse1(right) + reverse1(left); } }