2015-04-12 86 views
1

我需要製作一個程序,它接受用戶輸入的句子,並用正確的格式和標點符號將其顛倒。顛倒java中的字符串數組

例如:快速的棕色狐狸跳過懶惰的狗。

結果:「狗懶得跳過狐狸棕色快。」

我已經看到了解決方案,我可以通過每次詢問用戶1個單詞來得到正確答案。然而,我們特別要求僅向用戶詢問句子,然後該程序完成剩下的工作。所以程序必須確定數組的大小,並將字符串中的每個單詞分配給數組中的一個值(我猜?)。

到目前爲止,這是我的,但我認爲我需要使用stringBuffer,但我不知道如何實現這一點。

public class ReverseSentence { 

    public static void main(String[] args) { 






     String[] sentence = new String[]{IO.readString()}; 

     for(int counter = 0; counter < sentence.length; counter++){ 
      System.out.println("Enter Sentence"+(counter+1)); 
      sentence[counter] = IO.readString(); 
     } 

     System.out.println("The Reversed Sentence is:"); 
     for(int counter = sentence.length - 1; counter >= 0; counter--){ 
      System.out.print(sentence[counter]); 

     } 





    } 

} 

這不是一個家庭作業,只爲即將到來的考試實踐中的一些問題,所以一個完整的解決方案是好的,但如果可能的話,帶註釋行,所以我可以看到你是如何做到的。

+0

什麼是IO。readString()'return?一個詞還是整個句子? – Bubletan

回答

2

你有兩個不同的問題:

  1. 扭轉了一句
  2. 資本句子

讓我們做的第一部分拳頭:

public static String reverseSentence(final String sentence) { 
    final Pattern pattern = Pattern.compile("[^A-Za-z']+"); 
    final List<String> words = pattern.splitAsStream(sentence) 
      .map(String::toLowerCase) 
      .collect(toList()); 
    final StringBuilder reversed = new StringBuilder(); 
    final ListIterator<String> i = words.listIterator(words.size()); 
    reversed.append(i.previous()); 
    while (i.hasPrevious()) { 
     reversed 
       .append(" ") 
       .append(i.previous()); 
    } 
    reversed.append("."); 
    return reversed.toString(); 
} 

仔細檢查代碼:

public static void main(String[] args) throws Exception { 
    System.out.println(reverseSentence("The quick brown fox jumps over the lazy dog")); 
} 

狗懶過狐狸跳快速褐色的。

好了,現在我們需要大寫第一個字:

我們只需要使用此方法的第一個字:

public static String reverseSentence(final String sentence) { 
    final Pattern pattern = Pattern.compile("[^A-Za-z']+"); 
    final List<String> words = pattern.splitAsStream(sentence) 
      .map(String::toLowerCase) 
      .collect(toList()); 
    final StringBuilder reversed = new StringBuilder(); 
    final ListIterator<String> i = words.listIterator(words.size()); 
    reversed.append(capitalise(i.previous())); 
    while (i.hasPrevious()) { 
     reversed 
       .append(" ") 
       .append(i.previous()); 
    } 
    reversed.append("."); 
    return reversed.toString(); 
} 

檢查一遍:

狗懶過狐狸棕快速的。

+0

字面上是在問另外兩個人如何實施大寫。這裏的一切看起來都很完美,而且效果很棒要通過調試器運行幾次才能完全理解它。非常感謝。 – Blake

2

你可以嘗試這樣的:

public static String reverseString(String input) { 
    //from input to this method 
    // split input with space and store words 
    // in a collection if input is not empty 
    Deque<String> words = new ArrayDeque<>(); 
    for (String word: input.split(" ")) { 
     if (!word.isEmpty()) { 
      words.addFirst(word); 
     } 
    } 

     //now build output in reverse order of 
     // addition to collection if input is not empty 
    StringBuilder result = new StringBuilder(); 
    while (!words.isEmpty()) { 
     result.append(words.removeFirst()); 
     if (!words.isEmpty()) { 
      result.append(" "); 
     } 
    } 
    return result.toString(); 
} 
+0

這不會正確格式化該句子。 – Bubletan

+1

@Bubletan: - 我猜OP可以添加那個。無論如何要大寫的字符串的第一個字,你只需要添加這一行....'字符串輸出= input.substring(0,1).toUpperCase()+ input.substring(1);' –

0

試試這個。此代碼將使用句號(。)輸出所有格式的輸出。並仔細閱讀評論。

Scanner inp = new Scanner(System.in); 

String s1 = inp.nextLine(); 
String s2[] = s1.split(" "); 
boolean full_stop = false; 
// Printing first character of last string in upper case 
System.out.print(Character.toUpperCase(s2[s2.length - 1].charAt(0))); 
// Printing rest of the character of last string 
if (s2[s2.length - 1].contains(".")) {// checking that (.) is exists then print without (.) 
    System.out.print(s2[s2.length - 1].substring(1,s2[s2.length - 1].length() - 1) + " "); 
    full_stop = true; 
} else { 
    System.out.print(s2[s2.length - 1].substring(1, s2[s2.length - 1].length()) + " "); 
} 
for (int i = s2.length - 2; i >= 0; i--) { 
    if (i > 0) { 
     System.out.print(s2[i] + " "); 
    } else { 
     System.out.print(Character.toLowerCase(s2[i].charAt(0)));//converting first string character to lower case 
     System.out.print(s2[i].substring(1,s2[i].length()));// last string must not have space after that 
    } 
} 
if (full_stop) {// printing (.) if exists 
    System.out.println("."); 
}