题目描述
翻转一个句子:“student. a am I”。————》 “I am a student.”
解法
用split/concat/trim等string类方法完成
public class Solution {
public String ReverseSentence(String str) {
if(str.trim().isEmpty()){
return str;
}
String[] temp = str.split(" ");
String result = "";
for(int i = temp.length - 1;i >= 0;i--){
result = result.concat(temp[i]);
result = result.concat(" ");
}
result = result.trim();
return result;
}
}```