Solved

reverse a word using stack

Posted on 2003-11-06
9
870 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
  • 3
  • 2
  • 2
  • +1
9 Comments
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
Sounds suspiciously like homework...
0
 
LVL 23

Expert Comment

by:brettmjohnson
Comment Utility
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
Comment Utility
...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
 
LVL 14

Accepted Solution

by:
StillUnAware earned 20 total points
Comment Utility
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
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
Comment Utility
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
Comment Utility
.
0
 

Author Comment

by:Rebecca2003
Comment Utility
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
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…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

771 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now