6. 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
enter
I am a student
Output
student a am I
analysis
1. The god of regular expression manipulation of strings, the strongest on the surface, there is no one
2. Use regular expressions to filter out non-letter characters during keyboard entry
3. [^a-zA-Z] is to match non-a-z or non-A-Z characters in the target string
4. ^ [a-zA-Z is to match the characters starting with a-z or A-Z in brackets in the target string
Code
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str;
while ((str = br.readLine()) != null) {
String[] arr = str.split("[^a-zA-Z]+");//利用正则表达式,去掉非字母
StringBuilder ss = new StringBuilder();
for (int i = arr.length - 1; i >= 0; i--) {
if (i != 0)
ss.append(arr[i]).append(" ");
}
System.out.println(ss.toString().trim());
}
}
}