<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # Java 程序:反轉`String`中的單詞 > 原文: [https://beginnersbook.com/2017/09/java-program-to-reverse-words-in-a-string/](https://beginnersbook.com/2017/09/java-program-to-reverse-words-in-a-string/) 該程序反轉字符串的每個單詞并將反轉的字符串顯示為輸出。例如,如果我們輸入一個字符串`"reverse the word of this string"`,那么程序的輸出將是:`"esrever eht drow fo siht gnirts"`。 要了解此程序,您應該具有以下 [Java 編程](https://beginnersbook.com/java-tutorial-for-beginners-with-examples/)主題的知識: 1. [Java `for`循環](https://beginnersbook.com/2015/03/for-loop-in-java-with-example/) 2. [Java `String split()`方法](https://beginnersbook.com/2013/12/java-string-split-method-example/) 3. [Java `String charAt()`方法](https://beginnersbook.com/2013/12/java-string-charat-method-example/) ## 示例:使用方法反轉`String`中的每個單詞 在本程序中,我們首先使用`split()`方法將給定的字符串拆分為子字符串。子串存儲在`String`數組`words`中。程序然后使用反向的`for`循環反轉子串的每個單詞。 ```java public class Example { public void reverseWordInMyString(String str) { /* The split() method of String class splits * a string in several strings based on the * delimiter passed as an argument to it */ String[] words = str.split(" "); String reversedString = ""; for (int i = 0; i < words.length; i++) { String word = words[i]; String reverseWord = ""; for (int j = word.length()-1; j >= 0; j--) { /* The charAt() function returns the character * at the given position in a string */ reverseWord = reverseWord + word.charAt(j); } reversedString = reversedString + reverseWord + " "; } System.out.println(str); System.out.println(reversedString); } public static void main(String[] args) { Example obj = new Example(); obj.reverseWordInMyString("Welcome to BeginnersBook"); obj.reverseWordInMyString("This is an easy Java Program"); } } ``` **輸出:** ```java Welcome to BeginnersBook emocleW ot kooBsrennigeB This is an easy Java Program sihT si na ysae avaJ margorP ``` 看看這些相關的 [java 程序](https://beginnersbook.com/2017/09/java-examples/): 1. [Java 程序:反轉數字](https://beginnersbook.com/2014/01/java-program-to-reverse-a-number/) 2. [Java 程序:反轉字符串](https://beginnersbook.com/2017/09/java-program-to-reverse-a-string-using-recursion/) 3. [Java 程序:反轉數組](https://beginnersbook.com/2017/09/java-program-to-reverse-the-array/) 4. [Java 程序:使用循環檢查 Palindrome 字符串](https://beginnersbook.com/2014/01/java-program-to-check-palindrome-string/)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看