2014-01-06 20 views
1

我想按字反向字符串中的字。但我遇到了一些麻煩。我知道很多人使用StringBuilder來解決這個問題,但我想在沒有它的情況下嘗試一下。在一個字符串中按字反向字

輸入:

Hi there 

輸出:

iH ereht 

目前,我的輸入字符串的最後一個單詞停止。我認爲這是由於在我的代碼中,代碼的反向部分僅在檢測到' '或空間時纔會反轉。當我到達字符串的末尾時,我通過執行反轉部分來改變它。 (i == len)但是,這似乎並沒有解決問題。我認爲在我的ifelse if陳述和for loop也有一些邏輯錯誤。我想知道是否有人能指引我走向正確的方向。

,我一直在努力

測試用例字符串是

"Hi there Mr.Doge!"

我得到現在的輸出是

iH ereht < - 在字符串的結尾空間。

隨着代碼的進行,最後一個字(Mr.Doge!)被存儲到temp中,但打印出的文本不是反轉的。

這裏是輸出,當我編譯代碼:

0 
H 
1 
Hi 
2 
i 
iH 
3 
t 
4 
th 
5 
the 
6 
ther 
7 
there 
8 
iH e 
iH er 
iH ere 
iH ereh 
iH ereht 
9 
M 
10 
Mr 
11 
Mr. 
12 
Mr.D 
13 
Mr.Do 
14 
Mr.Dog 
15 
Mr.Doge 
16 
Mr.Doge! 
iH ereht 

我的代碼:

public static String reverseWord(String str){ 
    int len = str.length(); 
    String reverse = "", temp = ""; 

    for (int i = 0; i < len; i++) { 
     System.out.println(i); 
     if (str.charAt(i) != ' '){ 
      temp += str.charAt(i); 
      System.out.println(temp); 
     } 
     else if (str.charAt(i) == ' ' || i == len){ 
     //if (str.charAt(i) == ' ') { 
      for (int j = temp.length() - 1; j >= 0; j--) {  // reverse 
       reverse += temp.charAt(j);      // append in reverse 
       System.out.println(reverse); 
      } 
      reverse += ' '; 
      temp = ""; 
     } 
    } 
    return reverse; 
} 
+0

爲什麼你不希望使用'StringBuilder' ?如果您使用字符串,則每次追加字符串時都會創建一個新字符串並降低效率。至少在C#中,但我相信這同樣適用於Java。 – pcnThird

+0

在上面的代碼中,'for'循環的條件是'i Manish

+3

我不是所有charats和數字的粉絲。也許[這種方法](https://ideone.com/D2ZU3W)可能對你有幫助。 –

回答

3

通過一些修改,這必須奏效。查看代碼中的評論,看看我修改了什麼。

代碼:

public static void main(String[] args) 
{ 
    System.out.println(reverseWord("Hello world Liondancer")); 
} 

public static String reverseWord(String str) 
{ 
    int len = str.length(); 
    String reverse = "", temp = ""; 

    for (int i = 0; i < len; i++) { // i == len comparison is unuseful since 'i' won't never be 'len' 
     if (str.charAt(i) != ' ') { 
      temp = str.charAt(i) + temp; // What you did, but add the current character first, THIS IS THE REVERSE!!! 
     } else if (str.charAt(i) == ' ') { 
      reverse += temp + " "; 
      temp = ""; 
     } 
    } 
    reverse += temp; // Added this outside the loop to add last word stored in 'temp' 
    return reverse; 
} 

輸出:

olleH dlrow recnadnoiL 

注:

我刪除了嵌套for,因爲它是沒有必要的。

+0

感謝您的幫助!這幫了很大的忙! – Liondancer

0

你的循環之外的反向,並檢查你沒有在最後一個字(像這樣) -

public static String reverseWord(String str) { 
    int len = str.length(); 
    String reverse = "", temp = " "; 

    for (int i = 0; i < len; i++) { 
    if (str.charAt(i) != ' ') { 
     temp += str.charAt(i); 
    } else if (str.charAt(i) == ' ' || i == len) { 
     if (i + 1 < len) { 
     for (int j = temp.length() - 1; j >= 0; j--) { // reverse 
      reverse += temp.charAt(j); // append in reverse 
     } 
     temp = " "; 
     } else { 
     temp = ""; 
     } 
    } 
    } 
    for (int j = temp.length() - 1; j >= 0; j--) { // reverse 
    reverse += temp.charAt(j); // append in reverse 
    } 
    return reverse; 
} 
+0

表示只翻轉字符串中的第一個單詞 – Liondancer

0

如果我要這樣做,我只會將整個字符串存儲在一個數組列表中。 然後說:

for(i=0;i<len;i++) 
    temp.size()=len; 
    temp(length-i)=str(i); 
    print temp; 
1

嘗試這種變化

for (char c : str.toCharArray()) { 
     if (c != ' '){ 
      temp = c + temp; 
     } else { 
      reverse += temp + ' '; 
      temp = ""; 
     } 
    } 
    reverse += temp; 
0

您可以使用這種方式太環......

public String reverseString(String str){ 
     String reverse=""; 

     for(int i=str.length()-1; i>=0; i--){ 

      reverse = reverse + str.charAt(i); 

     } 
      return reverse; 

    }