-1

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

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<String> stack= new Stack<String>();

        sentence.split(" ");

        for(int i=0;i<sentence.length(); i++)
        {
            stack.push(sentence.substring(i, i+1));
        }

        while(!stack.isEmpty())
        {
            stringReversed += stack.pop();
        }

        System.out.println("Reverse is: " + stringReversed);


        return reverse;
    }

}
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
user3071909
  • 15
  • 3
  • 3
  • 9

3 Answers3

2

I will type an expatiation so you can still get the experience of writing the code, rather than me just giving you the code.

First create a Stack of Characters. Then use add each character in the String to the Stack, starting with the first char, then the second, and so on. Now either clear the String or create a new String to store the reversed word. Finally, add each character from the Stack to the String. This will pull the last character off first, then the second to last, and so on.

Note: I believe you have to use the Character wrapper class, rather than the primitive char; I may be incorrect about that though.

If you aren't familiar with how Stacks work, here is a nice interactive tool to visualize it: http://www.cise.ufl.edu/~sahni/dsaaj/JavaVersions/Stacks/AbstractStack/AbstractStack.htm

hcps-tenembasj
  • 345
  • 3
  • 9
0

Change:

Stack<String> stack = new Stack<String>(); 

to be

Stack<Character> stack = new Stack<Character>();

and refactor your methods code as necessary; i.e.

What is the easiest/best/most correct way to iterate through the characters of a string in Java?

Community
  • 1
  • 1
wulfgarpro
  • 6,666
  • 12
  • 69
  • 110
0

I did it with a different kind of stack, but I suspect this might help

private static String reverseWord(String in) {
  if (in.length() < 2) {
    return in;
  }
  return reverseWord(in.substring(1)) + in.substring(0, 1);
}

private static String reverseSentence(String in) {
  StringBuilder sb = new StringBuilder();
  StringTokenizer st = new StringTokenizer(in);
  while (st.hasMoreTokens()) {
    if (sb.length() > 0)
      sb.append(' ');
    sb.append(reverseWord(st.nextToken()));
  }
  return sb.toString();
}

public static void main(String[] args) {
  String sentence = "Hi dog cat";
  String expectedOutput = "iH god tac";
  System.out.println(expectedOutput
      .equals(reverseSentence(sentence)));
}

Outputs

true
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249