题解 | #单词倒排#
单词倒排
http://www.nowcoder.com/practice/81544a4989df4109b33c2d65037c5836
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String line = scan.nextLine();
int sign = 0;
int end = 0;
char[] chrs = line.toCharArray();
StringBuffer sb = new StringBuffer("");
for (int i = chrs.length - 1; i > -1; i--) {
char chr = chrs[i];
if ((chr >= 'a' && chr <= 'z') || (chr >= 'A' && chr <= 'Z')) {
if (sign == 0) {
sign = 1;
end = i + 1;
}
} else {
if (sign == 1) {
sign = 0;
sb.append(line.substring(i + 1, end));
sb.append(" ");
}
}
}
if (sign == 1) {
sb.append(line.substring(0, end));
}
System.out.println(sb);
}
}