2017-01-10 63 views
-5

如何在input_1中反轉整個段落,除了input_2中的單詞? 輸出應該是這樣的....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

歡迎來到Stack Overflow!這裏的用戶通常更喜歡回答顯示作者已經嘗試過的問題:) – TrojanByAccident

+0

告訴我們你得到的東西! – abhiarora

回答

0

模糊作爲您的問題是關於輸入表達式的語法,我會回答它,假設您輸入第二個字符串(字符串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)方法在由分隔符分隔的String中創建一個子字符串數組,但爲了清晰和基本級別的理解,我已經展示了該方法的實現。

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()); 
    } 
} 

你要了解更多關於正則表達式處理功能工作在這樣的字符串處理例子中