2016-11-19 1 views
0

私は文字列の順序を逆にするプログラム、つまり句読点も書こうとしています。しかし、私の後方の文字列が印刷されます。最後の単語の末尾にある句読点は、個々の文字として扱われるのではなく、単語の最後にとどまります。文字列の最後から句読点を最初に移動するにはどうすればよいですか?

最終句読点を最後の単語から分割して移動するにはどうすればよいですか?

例: 入力時:こんにちは私の名前はjasonです!

私が欲しい!:ジェイソン:ジェイソンは、代わりに私が手こんにちは私の

名前です!入力が!が含まれている場合(2)、その後にそれを接頭辞

(1)入力に!文字の

を確認:名前が

こんにちは、私の
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

各単語に、文字ではないものが「endsWith」であるかどうかを確認してください。その場合は、単語の先頭に移動します。そして、大喜び。 –

答えて

0

であるあなたは、以下の手順を実行する必要があり変数空の出力文字列

(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はい、あなたは正しいです、句読点の文字を配列に定義し、charをチェックして追加することで答えを更新しました。 – developer

+0

私はいくつかのコメントを編集し、最後の 'if'ブロックを忘れてしまったと思います。そして、あなたの説明から '!'を始めから変更して '?に置き換えてください。 。 ! ' – 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 

これは、そのコードの一部、またはあなたの未来の自己に取り組んで次の人のために理解することはとても簡単です。

これは、すべての句読点を移動することを前提としています。あなたが唯一の入力文字列の末尾に1が必要な場合は、入力し、それをカットする必要があるだろう、並べ替えを行い、最終的に文字列の先頭にそれを置く:

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''は、区切り文字列内の文字のいずれかと一致するすべての文字( '' new 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を参照してください。

関連する問題