我正在尝试编写一个程序来反转字符串的顺序,甚至是标点符号。但是当我向后打印字符串时。最后一个单词末尾的标点符号位于单词的末尾,而不是被视为单个字符。
如何从最后一个单词中分割结束标点符号,以便我可以移动它?
例如: 当我输入:你好我的名字是杰森!
我想:!杰森的名字是我的Hello
相反,我得到:杰森!是我的名字import java.util.*;
class Ideone
{
public static void main(String[] args) {
Scanner userInput = new Scanner(System.in);
System.out.print("Enter a sentence: ");
String input = userInput.nextLine();
String[] sentence= input.split(" ");
String backwards = "";
for (int i = sentence.length - 1; i >= 0; i--) {
backwards += sentence[i] + " ";
}
System.out.print(input + "\n");
System.out.print(backwards);
}
}
答案 0 :(得分:2)
手动重新排列字符串很快就会变得复杂。通常更好(如果可能)编码您想要做的,而不是 你想要做什么。
String input = "Hello my name is jason! Nice to meet you. What's your name?";
// this is *what* you want to do, part 1:
// split the input at each ' ', '.', '?' and '!', keep delimiter tokens
StringTokenizer st = new StringTokenizer(input, " .?!", true);
StringBuilder sb = new StringBuilder();
while(st.hasMoreTokens()) {
String token = st.nextToken();
// *what* you want to do, part 2:
// add each token to the start of the string
sb.insert(0, token);
}
String backwards = sb.toString();
System.out.print(input + "\n");
System.out.print(backwards);
输出:
Hello my name is jason! Nice to meet you. What's your name?
?name your What's .you meet to Nice !jason is name my Hello
对于处理该段代码或未来自我的下一个人来说,这将更容易理解。
这假定您要移动每个标点符号。如果您只想要输入字符串末尾的那个,那么您必须将其从输入中删除,进行重新排序,最后将其放在字符串的开头:
String punctuation = "";
String input = "Hello my name is jason! Nice to meet you. What's your name?";
System.out.print(input + "\n");
if(input.substring(input.length() -1).matches("[.!?]")) {
punctuation = input.substring(input.length() -1);
input = input.substring(0, input.length() -1);
}
StringTokenizer st = new StringTokenizer(input, " ", true);
StringBuilder sb = new StringBuilder();
while(st.hasMoreTokens()) {
sb.insert(0, st.nextToken());
}
sb.insert(0, punctuation);
System.out.print(sb);
输出:
Hello my name is jason! Nice to meet you. What's your name?
?name your What's you. meet to Nice jason! is name my Hello
答案 1 :(得分:0)
您需要按照以下步骤操作:
(1)检查输入中的!
字符
(2)如果输入包含!
,则将其作为空输出字符串变量
(3)如果输入不包含!
,则创建空输出字符串变量
(4)拆分输入字符串并以相反的顺序迭代(你已经这样做了)
您可以参考以下代码:
public static void main(String[] args) {
Scanner userInput = new Scanner(System.in);
System.out.print("Enter a sentence: ");
String originalInput = userInput.nextLine();
String backwards = "";
String input = originalInput;
//Define your punctuation chars into an array
char[] punctuationChars = {'!', '?' , '.'};
String backwards = "";
//Remove ! from the input
for(int i=0;i<punctuationChars.length;i++) {
if(input.charAt(input.length()-1) == punctuationChars[i]) {
input = input.substring(0, input.length()-1);
backwards = punctuationChars[i]+"";
break;
}
}
String[] sentence= input.split(" ");
for (int i = sentence.length - 1; i >= 0; i--) {
backwards += sentence[i] + " ";
}
System.out.print(originalInput + "\n");
System.out.print(input + "\n");
System.out.print(backwards);
}
答案 2 :(得分:0)
与其他答案一样,需要先将标点符号分开,然后对单词重新排序,最后将标点符号放在开头。
你可以利用String.join()和Collections.reverse(),String.endsWith()来获得更简单的答案......
String input = "Hello my name is jason!";
String punctuation = "";
if (input.endsWith("?") || input.endsWith("!")) {
punctuation = input.substring(input.length() - 1, input.length());
input = input.substring(0, input.length() - 1);
}
List<String> words = Arrays.asList(input.split(" "));
Collections.reverse(words);
String reordered = punctuation + String.join(" ", words);
System.out.println(reordered);
答案 3 :(得分:0)
以下代码应该适合您
disable
答案 4 :(得分:0)
不要用空格分开;按字界划分。然后你不需要关心标点符号甚至放回空格,因为你也只是反过来了!
它只有一行:
Arrays.stream(input.split("\\b"))
.reduce((a, b) -> b + a)
.ifPresent(System.out::println);
请参阅live demo。