题解 | #字符串变形#
字符串变形
https://www.nowcoder.com/practice/c3120c1c1bc44ad986259c0cf0f0b80e
split()分割,注意limit=-1这个参数
import java.util.Scanner; public class Trans { public String trans(String s, int n) { // write code here String[] strs = s.split(" "); StringBuilder stringBuilder = new StringBuilder(); for (int i = strs.length-1; i >= 0; i--) { for (int j = 0; j < strs[i].length(); j++) { if (strs[i].charAt(j)<97){ stringBuilder.append((char) (strs[i].charAt(j)+32)); } if (strs[i].charAt(j)>=97){ stringBuilder.append((char)(strs[i].charAt(j)-32)); } } stringBuilder.append(" "); } String S = stringBuilder.charAt(stringBuilder.length()-1)==' '?stringBuilder.deleteCharAt(stringBuilder.length()-1).toString():stringBuilder.toString(); return S; }