题解 | #句子逆序#
句子逆序
http://www.nowcoder.com/practice/48b3cb4e3c694d9da5526e6255bb73c3
import java.util.Scanner;
public class Main{
public static void main(String []args){
Scanner in = new Scanner(System.in);
String str = new String(in.nextLine());
if(1 <= str.length() && str.length() <= 1000){
int end = str.length() - 1;
int start = 0;
for(int i = str.length() - 1; i >= 0; i--){
if(str.charAt(i) == " ".charAt(0) || i == 0){
if(i == 0){
/*for(int j = i; j <= end; j++){
System.out.print(str.charAt(j));
}*/
System.out.print(str.substring(i, end + 1));
break;
}
else{
//旧方法,效率较低,其实不用循坏,直接调用substring()即可
/*for(int j = i + 1; j <= end; j++){
System.out.print(str.charAt(j));
}*/
System.out.print(str.substring(i + 1, end + 1));
end = i - 1;
System.out.print(" ");
}
}
}
}
}
}