2016-11-19 229 views
0

我正在尝试编写一个反转字符串顺序的程序,即使是标点符号。但是当我的后面的字符串打印。最后一个单词末尾的标点符号停留在单词的末尾,而不是作为单独的字符处理。如何将字符串末尾的标点符号移动到开头?

如何将最后一个单词的末尾标点符号分开,以便我可以移动它?

例如: 当我输入:你好我的名字是杰森!

我想:!杰森是我的名字我好你好

反而我得到了:杰森!被命名我喂

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

你的反向定义怎么可能? '!'不是一个单独的词来获得你想要的。它可以完成,但你的反向字符串顺序思维是错误的。 – SkrewEverything

+0

在每个单词上,检查它是否“结束”某个不是字母的东西。如果是这样,将其移动到单词的开头。瞧。 –

回答

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); 
    } 
+0

标题特别说*“将字符串末尾的标点符号移到开头”*。他只是提供了一个使用'!'的例子,并且你还应该在字符串中间考虑标点符号。请编辑你的答案和代码,以满足所有符号(这是更通用的方式) – SkrewEverything

+0

@SkrewEverything是的,你是对的,通过定义标点符号到数组中并检查字符和添加来更新我的答案。 – developer

+0

我想你忘了编辑一些评论和最后'如果'块。并且请在开始时将'!'从你的解释中更换,并用'?'替换它。 。 !' – SkrewEverything

1

手动清理字符串往往在任何时间变得复杂。这通常是更好的(如果可能)代码什么你想要做的,而不是如何你想做的。

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 
+0

所以我可以深入了解,你能详细说明这一行吗? : String token = st.nextToken(); –

+0

''StringTokenizer''将在每个符合分隔符字符串中的一个字符的字符处分割输入(新的StringTokenizer中的第二个参数(input,“。?!”,true);'',所以''又名空格,'''''','''''和''!''在这个例子中)。每次调用st.nextToken()都会返回下一个分割字符串 - 这个类调用这些分片标记。 – duckstep

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); 
0

下面的代码应该为你工作

import java.util.regex.Matcher; 
import java.util.regex.Pattern; 
public class ReplaceSample { 
    public static void main(String[] args) { 
     String originalString = "TestStr?"; 
     String updatedString = ""; 
     String regex = "end\\p{Punct}+|\\p{Punct}+$"; 
     Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE); 
     Matcher matcher = pattern.matcher(originalString); 
    while (matcher.find()) { 
      int start = matcher.start(); 
      updatedString = matcher.group() + originalString.substring(0, start);<br> 
     } 
     System.out.println("Original -->" + originalString + "\nReplaced -->" +  updatedString); 
    } 
} 
0

不要用空格分开;按字词划分。那么你不需要关心标点符号甚至把空格放回去,因为你也只是将它们反转过来!

而且它只有1行:

Arrays.stream(input.split("\\b")) 
    .reduce((a, b) -> b + a) 
    .ifPresent(System.out::println); 

live demo

相关问题