2016-11-19 160 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

相關問題