[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 942
  • Last Modified:

reverse a word using stack

How do reverse a word by using a stack? How many stack should i use?

izzit top into a stack, then already can topAndPop() to print it out?

Stack stk = new ArrayStack();
while(!stk.isEmpty())
System.out.println(stk.topAndPop());
0
Rebecca2003
Asked:
Rebecca2003
  • 3
  • 2
  • 2
  • +1
1 Solution
 
CEHJCommented:
Sounds suspiciously like homework...
0
 
brettmjohnsonCommented:
One stack will do.  The nature (indeed definition) of a stack is "Last In, First Out" (LIFO).
So items are removed from the stack in the reverse order in which they were pushed
onto the stack.  Therein is the mechanism for "reversing the characters in a word".

First, I would use java.util.Stack instead of the Apache ArrayStack class.  You might as
well stick to the standard classes for such trivial problems.  Create the Stack, iterate
over the string, pushing the individual characters on the stack (see java.lang.Character).
After the entire word has been pushed on the stack.  Pop of the characters one at a time,
constructing the new word in reverse order.

0
 
CEHJCommented:
...but here are some clues

1. You can only push objects on a Stack
2. Strings are composed of (arrays of) char, so it would be natural for the String class to have methods that reflect this reality
3. The object version of char is Character, and it would be natural for that relation to be reflected in the methods of the Character class
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
StillUnAwareCommented:
lets say string s equls to word 'Hello' then
the reverse oder of it is

//stack.java
import java.util.*;

public class stack{
  public static void main(String[] args){
    String s = "Hello";
            Stack stk = new Stack();

    for( int i = 0; i < s.length(); i++ )
              stk.push(new Character(s.charAt(i)));
            s = "";
            while( !stk.empty() )
              s = s + (Character)(stk.pop());
            System.out.println(s);
  }
}
0
 
CEHJCommented:
StillUnAware, you are obviously StillUnAware about the fact that the Member Agreement states that we cannot code people's homework
0
 
StillUnAwareCommented:
Don't be so mad :), I want my first expert points. It must be homework, but this one is pretty easy, let it be an example for future exercises,

peace
0
 
Rebecca2003Author Commented:
.
0
 
Rebecca2003Author Commented:
No...this's not my homework question, my howework question is actually asking me to write a palindrome program.  At first i dunno whether is suitable to use stack or queue. By now, i user 1 stack n 1 queue to solved my program already. Beisdes this, i also noticed that by using 3 stacks is still work.  That question that i asked is quite easy rite... how can it be a homework? So if like this case, if i cant ask this, i really cant work out my program. Thanx StillUnAware for giving me some idea.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now