题目描述
将一个英文语句以单词为单位逆序排放。例如“I am a boy”,逆序排放后为“boy a am I”
所有单词之间用一个空格隔开,语句中除了英文字母外,不再包含其他字符
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
String output = "";
while(sc.hasNext()){
String input = sc.nextLine();
output = reverse(input);
System.out.println(output);
}
}
public static String reverse(String sentence){
if(sentence==" "){
return " ";
}
if(sentence.length()==0){
return null;
}
String[] senArr = sentence.split(" ");
String outputStr = "";
for(int i=senArr.length-1;i>=0;i--){
if(i==0){
outputStr+=senArr[i];
}else{
outputStr+=senArr[i]+" ";
}
}
return outputStr;
}
}