例如:
Are you ready ?
翻转后:
? ready you Are
public class 字符串按单词翻转 {
public static void main(String[] args) {
String s = "Are you ready ?";
System.out.println(reverse(s));
}
static String reverse(String S) {
String s1 = reverseString(S);
String [] words = s1.split(" ");
StringBuffer sb = new StringBuffer();
for (int i = 0; i <words.length ; i++) {
sb.append(reverseString(words[i])+" ");
}
return sb.deleteCharAt(sb.length()-1).toString();
}
static String reverseString(String S) {
StringBuffer stringBuffer = new StringBuffer(S);
return stringBuffer.reverse().toString();
}
}