/*
* 题目分析: 提取有效字符并倒序
* 解题思路:
* 1. 遍历字符串, 将非法字符替换为空格
* 2. 以空格作为pattern将字符串分割为字符串数组
* 2. 倒序输出字符串数组即可
* 提交失败:
* 1. splite()切割时,连续的空格之间会产生空字符串占一个数组元素,导致单词间打印多个空格
* 2. 忘记删掉调试打印的内容了...
* 3. 空格空格空格, 不知道哪里多了个空格...重新用ArrayList写了...
*/
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNextLine()) {
ArrayList<String> list = new ArrayList<String>();
// 在字符串后追加1个非法字符, 否则最后一个单词无法被记录
String str = sc.nextLine() + "0";
int start = 0;
// 初始状态为未存储, 有合法字符再置为false, 否则当字符串前置非法字符时会打印额外的空格
boolean isSaved = true;
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if (ch >= 'A' && ch <= 'Z' || ch >= 'a' && ch <= 'z') {
isSaved = false;
continue;
}
if (!isSaved) {
list.add(str.substring(start, i));
isSaved = true;
start = i;
}
start++;
}
for (int i = list.size() - 1; i > 0; i--) {
System.out.print(list.get(i) + " ");
}
System.out.println(list.get(0));
}
}
/*
// 开始的解题方法, 懒得去处理空格了(其实是不会)
// 改进: 调用splite()使用正则表达式 "[^A-Za-z]+"
public static void mainFailed(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNextLine()) {
StringBuffer sb = new StringBuffer(sc.nextLine());
for (int i = 0; i < sb.length(); i++) {
char ch = sb.charAt(i);
if (ch >= 'A' && ch <= 'Z' || ch >= 'a' && ch <= 'z') {
continue;
}
sb.replace(i, i + 1, " ");
}
String[] sts = sb.toString().split(" ");
for (int i = sts.length - 1; i > 0; i--) {
if (sts[i].equals("")) {
continue;
}
System.out.print(sts[i] + " ");
}
System.out.println(sts[0]);
}
}
*/
}