title: Java Using a stack to reverse the words in a sentence
body: <p>I am supposed to write a code that reads a sentence from the user and prints the characters of the words in the sentence backwards. It should include a helper method that takes a String as a parameter and returns a new String with the characters reversed. The individual words are reversed, for example the sentence "Hi dog cat". would print "iH god tac". I can make the entire sentence reverse but i cant figure out how to reverse individual words. Thanks!   Also, i know how to return the String once i have found it, but i just cant get the right string</p>
  
  <pre><code>import java.util.Scanner;
  import java.util.Stack;
  
  public class ReverseStack 
  {
      public static void main(String[] args)
      {
          String sentence;
  
          System.out.println("Enter a sentence: ");
          Scanner scan = new Scanner(System.in);
  
          sentence = scan.nextLine();
  
          String k = PrintStack(sentence);
      }
  
      private static String PrintStack(String sentence)
      {
          String reverse;
          String stringReversed = "";
  
          Stack&lt;String&gt; stack= new Stack&lt;String&gt;();
  
          sentence.split(" ");
  
          for(int i=0;i&lt;sentence.length(); i++)
          {
              stack.push(sentence.substring(i, i+1));
          }
  
          while(!stack.isEmpty())
          {
              stringReversed += stack.pop();
          }
  
          System.out.println("Reverse is: " + stringReversed);
  
  
          return reverse;
      }
  
  }
  </code></pre>