Solved

reverse a word using stack

Posted on 2003-11-06
9
912 Views
Last Modified: 2012-08-13
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
Comment
Question by:Rebecca2003
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
9 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 9696185
Sounds suspiciously like homework...
0
 
LVL 23

Expert Comment

by:brettmjohnson
ID: 9696217
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 9696243
...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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
LVL 14

Accepted Solution

by:
StillUnAware earned 20 total points
ID: 9700212
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 9700298
StillUnAware, you are obviously StillUnAware about the fact that the Member Agreement states that we cannot code people's homework
0
 
LVL 14

Expert Comment

by:StillUnAware
ID: 9712720
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
 

Author Comment

by:Rebecca2003
ID: 9714801
.
0
 

Author Comment

by:Rebecca2003
ID: 9714861
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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

690 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question