我编写了以下代码来反转输入字符串:
Scanner s = new Scanner(System.in);
System.out.println("Please enter a sentence:");
String sentence = s.nextLine();
String[] words = sentence.split(" ");
String reversedSentence = "";
for(int i = words.length - 1; i >= 0 ; i--)
{
reversedSentence += words[i] + " ";
}
System.out.println(reversedSentence);
然而它没有给我我想要的结果。我需要标点符号作为附加单词的一部分,但仍然要切换到单词的右侧。例如,如果您输入
“快速的棕色狐狸跳过懒狗”
我希望输出为
“狗懒得跳过狐狸褐快”
我实际得到的是:
狗“懒得过快跳狐狸褐色”
答案 0 :(得分:2)
如果您只想在输入的开头和结尾处理双引号,只需反转子字符串并稍后添加它们即可。 E.g。
if (sentence.startsWith("\"") && sentence.endsWith("\"")) {
sentence = sentence.substring(1, sentence.length()-1);
}
最后在分割,反转和连接打印之后:
System.out.println('"' + reversedSentence + '"');
另外2条建议:
1)你的for循环留下一个尾随空格。不要为最后一个字添加空格
2)您应该使用StringBuilder
来连接字符串。 E.g。
StringBuilder reversedSentence = new StringBuilder();
for (int i = words.length - 1; i > 0; i--) {
reversedSentence.append(words[i]).append(' ');
}
reversedSentence.append(words[0]);
System.out.println('"' + reversedSentence.toString() + '"');
答案 1 :(得分:0)
将字符串分解为数组并使用Collections.reverse(),如下所示:
public class StringReverse {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Please enter a sentence:");
String sentence = s.nextLine();
String[] array = sentence.replaceAll("\\“|”", "").split(" ");
StringBuilder sb = new StringBuilder();
List<String> list = Arrays.asList(array);
Collections.reverse(list);
array = (String[]) list.toArray();
for (int i = 0; i < array.length; i++) {
sb.append(array[i]).append(" ");
}
System.out.println(sentence.charAt(0)
+ sb.toString().replaceAll(" $", "")
+ sentence.charAt(sentence.length() - 1));
s.close();
}
}
使用 Java 8 Arrays.stream,它就像:
一样简单import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Scanner;
import java.util.stream.Collectors;
public class StringReverse {
Scanner s = new Scanner(System.in);
System.out.println("Please enter a sentence:");
String sentence = s.nextLine();
StringBuilder sb = new StringBuilder();
Arrays.stream(sentence.replaceAll("\\“|”", "").split(" "))
.collect(Collectors.toCollection(ArrayDeque::new))
.descendingIterator()
.forEachRemaining(e -> sb.append(e).append(" "));
System.out.println(sentence.charAt(0)
+ sb.toString().replaceAll(" $", "")
+ sentence.charAt(sentence.length() - 1));
s.close();
}
答案 2 :(得分:0)
如果标点符号也会打开和关闭。就像你的例子。你可以使用这样的东西:
非常脏。我稍后会编辑它。我不做多少。
String[][] punctuations = new String[][] {
{"(", ")"},
{"“", "”"}
};
for (String[] punctuation : punctuations) {
if (sentence.contains(punctuation[0])) {
int index_0 = sentence.indexOf(punctuation[0]);
int index_of_next_space = sentence.indexOf(" ", index_0);
String old_string_0 = sentence.substring(index_0, index_of_next_space);
String new_string_0 = old_string_0.replace(punctuation[0], "") + punctuation[1];
int index_1 = sentence.indexOf(punctuation[1]);
int index_of_last_space = sentence.lastIndexOf(" ", index_1);
String old_string_1 = sentence.substring(index_of_last_space+1, index_1 + 1);
String replaced_string_1 = punctuation[0] + old_string_1.replace(punctuation[1], "");
sentence = sentence.replace(old_string_0, new_string_0);
sentence = sentence.replace(old_string_1, replaced_string_1);
}
}
现在反转你的字符串。
输入:
(快速的棕色)狐狸跳过懒狗“
输出:
“狗懒得跳过狐狸(布朗的快)”
这可以改进。就像我之前说的那样,我不做多少:/。