Topic information
Invert all words in the string.
Description:
1. There are only 26 uppercase or lowercase English letters in the characters that make up a word;
2. Characters that do not form a word are regarded as word spacers;
3. The inverted word spacer is required to be represented by a single space; if there are multiple spacers between adjacent words in the original string, only one space spacer is allowed after the inverted conversion;
4. The maximum length of each word is 20 letters;
Example input: i2am@a student
Sample output: student a am i
coding
Method one , use buffer characters to store the words, and reverse the output of the words
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class ReverseWords {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String input;
// 输入非空且有实际内容值时进入循环
while ((input = br.readLine()) != null && input.trim().length() > 0) {
// 将输入内容转为字符数组
char[] chArr = input.toCharArray();
StringBuilder sb = new StringBuilder();
// 定义长度,标识连续有效单词字符长度
int mark = 0;
// 定义flag,为true标识为一个单词的连续字符
boolean flag = false;
for (int i=0; i<chArr.length; i++) {
// 若为有效字符,则flag置true,mark加1
if (Character.isLowerCase(chArr[i]) || Character.isUpperCase(chArr[i])) {
mark++;
flag = true;
// 若字符不为有效字符,且连续标识flag为true时,将之前有效字符输出
} else if (flag){
sb.append(chArr, i-mark, mark).append(" ");
mark = 0;
flag = false;
}
}
// 最后一个字符为单词字符场景,flag为true
if (flag) {
// 将有效的字符拼接
sb.append(chArr, chArr.length-mark, mark);
// 最后一个字符为非单词字符,此时flag为false,则之前缓冲字符最终符号为空格
} else {
// 去除最后一个空格
sb.toString().trim();
}
// 将拼接单词的缓冲字符串按照空格分隔为有效单词
String[] wordArr = sb.toString().split(" ");
sb = new StringBuilder();
// 单词逆序
for (int i=wordArr.length-1; i>=0; i--) {
sb.append(wordArr[i]).append(' ');
}
System.out.println(sb.substring(0, sb.length()-1));
}
}
}
Method 2 : Use the stack first-in-last-out principle to make words first-in-last-out, avoiding reverse processing of words
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Deque;
import java.util.LinkedList;
public class ReverseWords {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String input;
// 输入非空且有实际内容值时进入循环
while ((input = br.readLine()) != null && input.trim().length() > 0) {
// 将输入内容转为字符数组
char[] chArr = input.toCharArray();
// 定义长度,标识连续有效单词字符长度
int mark = 0;
// 定义flag,为true标识为一个单词的连续字符
boolean flag = false;
// 定义栈,利用栈先进后出的作用,最后输出单词
Deque<String> list = new LinkedList<>();
for (int i=0; i<chArr.length; i++) {
// 若为有效字符,则flag置true,mark加1
if (Character.isLowerCase(chArr[i]) || Character.isUpperCase(chArr[i])) {
mark++;
flag = true;
// 若字符不为有效字符,且连续标识flag为true时,将之前有效字符输出
} else if (flag){
// 有效单词入栈
list.push(String.valueOf(chArr, i-mark, mark));
mark = 0;
flag = false;
}
}
// 最后一个字符为单词字符场景,flag为true
if (flag) {
// 有效单词入栈
list.push(String.valueOf(chArr, chArr.length-mark, mark));
}
// 栈中有元素时,循环出栈
while (list.size() > 0) {
// 单词出栈并输出
System.out.print(list.pop());
// 若栈非空,输出单词间间隔符号空格
if (!list.isEmpty()) {
System.out.print(" ");
}
}
}
}
}