Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Per modifications to the original post, where the OP is now indicating that his goal is to reverse the letter order of the words within a sentence, but to leave the words in their initial positions.</p> <p>The simplest way to do this, I think, is to make use of the String <code>split</code> function, iterate through the words, and reverse their orders.</p> <pre><code>String[] words = sentence.split(" "); // splits on the space between words for (int i = 0; i &lt; words.length; i++) { String word = words[i]; System.out.print(reverseWord(word)); if (i &lt; words.length-1) { System.out.print(" "); // space after all words but the last } } </code></pre> <p>Where the method <code>reverseWord</code> is defined as:</p> <pre><code>public String reverseWord(String word) { for( int i = 0; i &lt; word.length(); i++) { stk.push(word.charAt(i)); } return stk.empty(); } </code></pre> <p>And where the <code>empty</code> method has been changed to:</p> <pre><code>public String empty() { String stackWord = ""; while (this.first != null) stackWord += this.pop(); return stackWord; } </code></pre> <p><strong>Original response</strong></p> <p>The original question indicated that the OP wanted to completely reverse the sentence.</p> <p>You've got a double-looping construct where you don't really need it.</p> <p>Consider this logic:</p> <ol> <li>Read each character from the input string and push that character to the stack</li> <li>When the input string is empty, pop each character from the stack and print it to screen.</li> </ol> <p>So:</p> <pre><code>for( int i = 0; i &lt; sentence.length(); i++) { stk.push(sentence.charAt(i)); } stk.empty(); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload