题解 | #字符串变形#
字符串变形
https://www.nowcoder.com/practice/c3120c1c1bc44ad986259c0cf0f0b80e
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param s string字符串 * @param n int整型 * @return string字符串 */ public String trans (String s, int n) { String[] tmp = s.split(" ", -1); StringBuilder stringBuilder = new StringBuilder(); for (int i = tmp.length - 1; i >= 0; i--) { stringBuilder.append(toLowerCase(tmp[i])); if (i != 0) { stringBuilder.append(" "); } } return stringBuilder.toString(); } public static String toLowerCase( String string ) { string = string.trim(); StringBuilder sb = new StringBuilder(); for ( int i = 0; i < string.length(); i++ ) { String sss = String.valueOf( string.charAt( i ) ); //判断sss是大写字母还是小写字母 if ( sss.matches( "[A-Z]" ) ) { sss = sss.toLowerCase(); } else if ( sss.matches( "[a-z]" ) ) { sss = sss.toUpperCase(); } else { System.out.println( "其他" ); } sb.append( sss ); } return sb.toString(); } }