题解 | #单词倒排#
单词倒排
https://www.nowcoder.com/practice/81544a4989df4109b33c2d65037c5836
基础语法
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < str.length(); i++) {
if ((str.charAt(i) >= 'A' && str.charAt(i) <= 'Z') || (str.charAt(i) >= 'a' &&
str.charAt(i) <= 'z')) {
sb.append(str.charAt(i));
} else {
sb.append(" ");
}
}
String[] newStr = sb.toString().split(" ");
for (int i = newStr.length - 1; i >= 0; i--) {
if (newStr[i] != " ") {
System.out.print(newStr[i] + " ");
}
}
}
}