题目:
将"here you are"翻转为"are you here"
思路:
首先将字符串翻转,然后找到每个单词,对每个单词进行翻转。
package 字符串问题;
/**
*将here you are翻转成are you here
*/
public class case08_按单词翻转字符串 {
public static void main(String[] args) {
String res=reverse("here you are");
System.out.println(res);
}
static String reverse(String s) {
String s1=reverseString(s);
//切割单词
String[] word=s1.split("\\s");
StringBuilder sb=new StringBuilder();
for(int i=0;i<word.length;i++){
sb.append(reverseString(word[i])+" ");
}
return sb.toString();
}
private static String reverseString(String s) {
StringBuilder sb=new StringBuilder(s);
return sb.reverse().toString();
}
}