思路之一如下:
1、按空格划分成字符串数组
2、数组元素顺序入栈
3、数组元素逆序出栈
4、大小写互转
import java.util.*; public class Transform { public String trans(String s, int n) { //空格划分成数组 String[] arr = s.split(" ",-1); //数组元素入栈 Stack<String> stack = new Stack<String>(); for(String str:arr){ stack.push(str); } //逆序出栈 StringBuilder sb = new StringBuilder(); while(!stack.isEmpty()){ sb.append(stack.pop()+" "); } //去除最后一个空格 sb.deleteCharAt(sb.length()-1); //大小写互换 return reverse(sb.toString()); } public String reverse(String s){ StringBuilder sb = new StringBuilder(); for (int i = 0; i < s.length(); i++) { char temp = s.charAt(i); if (temp >= 'a' && temp <= 'z') { temp = (char)(temp-32); }else if(temp >= 'A' && temp <= 'Z') { temp = (char)(temp+32); } sb.append(temp); } return sb.toString(); } }