2017-01-10 5 views
-5

input_2の単語を除くinput_1の段落全体を逆にするにはどうすればよいですか? 出力は、参考のために....このようjava逆順の文字列

String input_1="this is me.is this is me"; 
String input_2="is,me"; 

String output="me is siht is.me is siht"; 
+4

試しましたか? –

+2

ようこそスタックオーバーフロー!ここのユーザーは一般的に、著者がすでに試したことを示す質問に答えることを好む:) – TrojanByAccident

+0

私たちに何かを教えてください! – abhiarora

答えて

0

あなたの質問には、式の構文を入力することですが、逆にする必要のないすべての単語のリストとして2番目の文字列(コードスニペットの文字列input_2)を入力すると答えますコンマこれは私が理解しているように、あなたの質問に対する作業コードの解決策です。 私はVIM上で書いたように、インデントと書式をうまく覚えています。

import java.io.*; 
public class SelectReverse 
{ 
public static void main(String args[]) throws IOException 
{ 
    BufferedReader buf = new BufferedReader(new InputStreamReader(System.in)); 
    System.out.println("Enter the string to be selectively reversed"); 
    String str = buf.readLine(); 
    str = str.trim(); 
    str = " ".concat(str); 
    int len = str.length(); //length of first input string 
    System.out.println("Enter the words to be left intact during reversal"); 
    String s = buf.readLine(); 
    int l = s.length();  //length of second input string 
    int j = 0; 
    int k = len-1;   //stores index upto which substring is extracted 
    int wordcount = 1; 
    String result = ""; //stores output string 
    for(int i = 0; i<l; i++)  //counts no. of words which are not to be reversed 
    { 
     if(s.charAt(i) == ',')           
     { 
      wordcount++; 
     } 
    } 
    String arr[] = new String[wordcount]; //array of words not to be reversed 
    int counter = 0; 
    for(int i = 0; i<l; i++)           
    { 
     if(s.charAt(i) == ',') 
     { 
      String subs = s.substring(j, i); //extracting individual words from list of words not to be reversed 
      arr[counter] = subs; //adding them in the array 
      j = i+1; 
      counter++; 
     } 
    } 
    arr[counter] = s.substring(j); //adding last word (after the last comma) 
    boolean firstflag = false; 
    for(int i = len-1; i>=0; i--) 
    {   
     String substr; 
     if(str.charAt(i)==' ' || str.charAt(i)=='.') 
     { 
      if(firstflag == false) //substring is extracted till end of string only for the first extracted word 
      { 
       substr = str.substring(i+1); 
       firstflag = true; 
       k = i; 
      } 
      else 
      { 
       substr = str.substring(i+1, k); 
       k = i; 
      }   
      boolean flag = false; 
      for(int m = 0; m<wordcount; m++) 
      { 
       if(arr[m].equalsIgnoreCase(substr))//true if substring is not to be reversed, i.e. matches with any word in array 
       {     
        flag = true; 
       } 
      } 
      if(flag == true) //concatenating substring to output string without reversing 
      { 
       result = result+substr; 
       result = result+" "; 
      } 
      else //concatenating substring to output string after reversing 
      { 
       String reverse = ""; 
       int ln = substr.length(); 
       for(int n = ln-1; n>=0; n--) //reversing substring 
       { 
        char ch = substr.charAt(n); 
        String chstring = Character.toString(ch); 
        reverse = reverse+chstring; 
       } 
       result = result+reverse; 
       result = result+" "; 
      }   
     } 
    }  
    System.out.println(result); //displaying resultant output string 
    } 
} 

これは問題を解決するはずです。さらなる質問については、入力構文、正規表現、および問題文について詳しく説明してください。 .split(String、delimiter)メソッドを使用して、区切り文字で区切られた文字列に部分文字列の配列を作成することもできますが、分かりやすく基本的なレベルの理解のためにメソッドの実装を示しました。

0

使用次の例のようになります。 -

public class ReverseWords { 
    public static void main(String[] args) { 
     String input1 = "this is me.is this is me"; 
     String input2 = "is,me"; 
     String output = ""; 

     String[] wordsFromInput1 = input1.split("[\\s.]"); 
     String[] wordsFromInput2 = input2.split(","); 

     for (String tempWord1 : wordsFromInput1) { 
      boolean existInSecond = false; 
      for (String tempWord2 : wordsFromInput2) { 
       if (tempWord1.equals(tempWord2)) { 
        existInSecond = true; 
        break; 
       } 
      } 
      if (existInSecond) { 
       output += tempWord1 + " "; 
      } else { 
       output += new StringBuffer(tempWord1).reverse() + " "; 
      } 
     } 

     System.out.println(output.trim()); 
    } 
} 

は、あなたが仕事に正規表現文字列処理機能の詳細を学ばなければなりませんこのような文字列処理の例について説明します。

関連する問題