在一行上输入若干个字符串,每个字符串长度为
,仅由大小写字母构成,代表一个单词。单词间还夹杂了一定数量的非字母字符(但保证是可见字符),代表分隔符。
除此之外,保证总字符长度不超过
。
在一行上输出一个句子,代表以单词为单位逆序排放的结果。单词间使用单个空格分隔。
Nowcoder Hello
Hello Nowcoder
$bo*y gi!r#l
l r gi y bo
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String str = in.nextLine();
String[] split = str.split("[^a-zA-Z0-9]");
List<String> list = Arrays.asList(split);
Collections.reverse(list);
String join = String.join(" ", list);
System.out.println(join);
}
} import java.util.*; import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String str = in.nextLine(); StringBuilder builder = new StringBuilder(str); for (int i = 0; i < builder.length(); i++) { if (!Character.isLetterOrDigit(str.charAt(i))) { builder.setCharAt(i, ' '); } } String str1 = builder.toString(); String[] str2 = str1.split(" "); StringBuilder builder2 = new StringBuilder(); for (int i = str2.length - 1; i >= 0; i--) { builder2.append(str2[i] + " "); } String result = builder2.toString(); System.out.println(result); } }
import java.util.Scanner;
import java.util.regex.Pattern;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str1 = sc.nextLine();
char[] chars = str1.toCharArray();
Pattern p = Pattern.compile("[a-zA-Z]");
for (int i = 0; i < chars.length; i++) {
if(!p.matcher(String.valueOf(chars[i])).matches()){
chars[i] = ' ';
}
}
String s = String.valueOf(chars);
if(!p.matcher(s.substring(0,1)).matches()){
s = s.substring(1);
}
String[] s1 = s.split(" ");
StringBuilder result = new StringBuilder();
for (int i = s1.length-1; i >=0; i--) {
if(i!=0){
result.append(s1[i]).append(" ");
}else {
result.append(s1[i]);
}
}
System.out.println(result.toString());
}
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String s = in.nextLine(); // 原字符串
String res = ""; // 结果字符串
// 方法1
// for (int i = 0; i < s.length(); i++) {
// String t = "";
// while (i < s.length() && !Character.isLetter(s.charAt(i))) {
// i++;
// }
// while (i < s.length() && Character.isLetter(s.charAt(i))) {
// t += s.charAt(i);
// i++;
// }
// res = t + " " + res;
// }
// System.out.print(res);
// 方法2
// 利用正则 剔除其他字符 + 拆分 逆序输出 (上面 方法1 纯手动)
String[] sp = s.replaceAll("[^a-zA-Z]", " ").split(" ");
for (int i = sp.length - 1; i >= 0; i--) {
res = res + " " + sp[i];
}
System.out.print(res.substring(1));// 头多个空格
}
} import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String str = in.nextLine();
System.out.print(reverseWords(str));
}
public static String reverseWords(String str) {
String s = str.replaceAll("[^a-zA-Z0-9]"," ");
// 使用空格分割句子成单词数组
String[] words = s.split(" ");
// 创建一个StringBuilder用于构建逆序后的句子
StringBuilder sb = new StringBuilder();
// 逆序遍历单词数组
for (int i = words.length - 1; i >= 0; i--) {
// 将单词添加到StringBuilder
sb.append(words[i]);
// 如果不是最后一个单词,添加一个空格
if (i > 0) {
sb.append(" ");
}
}
// 返回逆序后的句子
return sb.toString();
}
} import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) {
String word = scanner.nextLine();
String str = "";
//特殊字符变为空格
for (int i = 0; i < word.length(); i++) {
char ch = word.charAt(i);
if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z')) {
str += ch;
} else {
str += " ";
}
}
String words[] = str.split(" ");
String reverseWord = "";
//倒置数据
for (int i = 0; i < words.length; i++) {
reverseWord += words[words.length - i - 1] + " ";
}
String reverseWords[] = reverseWord.split(" ");
String finalString = "";
for (int i = 0; i < reverseWords.length; i++) {
finalString += reverseWords[i] + " ";
}
// 将多余空格置换为一个空格
finalString = finalString.replaceAll("\\s+", " ");
System.out.println(finalString.trim());
}
scanner.close();
}
} import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String str = in.nextLine();
str = str.replaceAll("[^a-zA-Z]+"," ");
List<String> list = new ArrayList<>();
list = Arrays.asList(str.split(" "));
Collections.reverse(list);
String strResult = String.join(" ",list);
System.out.println(strResult);
}
} import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String str = in.nextLine();
List<String> list = new ArrayList<>();
String strTemp = new String();
for(int i = 0;i<str.length();i++){
char c = str.charAt(i);
if(c <= 'z' && c>='a'){
strTemp += c;
}else if(c <='Z' && c>='A'){
strTemp += c;
}else{
if(strTemp != null && strTemp.length()>0){
list.add(strTemp);
}
strTemp = "";
}
}
list.add(strTemp);
Collections.reverse(list);
String strResult = String.join(" ",list);
System.out.println(strResult);
}
} import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNextLine()) { // 注意 while 处理多个 case
String inString = in.nextLine();
List<String> arr = new ArrayList<>();
String s = "";
for (int i = 0 ; i < inString.length() ; i++) {
if (Character.isLetter(inString.charAt(i))) {
s += inString.charAt(i);
//对最后一个单词单独添加
if (!"".equals(s) && i == inString.length() - 1) {
arr.add(s);
}
} else {
if (!"".equals(s)) {
arr.add(s);
}
s = "";
}
}
//倒排
Collections.reverse(arr);
String res = "";
for (String part : arr) {
res += part + " ";
}
System.out.println(res);
}
}
} import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
String str = in.nextLine();
int right = str.length() - 1;
int left = right;
StringBuffer res = new StringBuffer();
while (right>=0) {
while (left >= 0 && Character.isLetter(str.charAt(left))) {
left--;
}
res.append(str.substring(left + 1, right + 1));
if(left != 0){
res.append(" ");
}
while(left>=0 && !Character.isLetter(str.charAt(left))){
left--;
}
right = left;
}
System.out.println(res.toString());
}
} 使用双指针解决,和H13句子逆序相同,一并解决 import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String str = in.nextLine();
String[] strs = str.split("[^a-zA-Z]");
for (int i = strs.length-1; i >= 0; i--) {
System.out.print(strs[i]+" ");
}
}
}