Solved

reverse a word using stack

Posted on 2003-11-06
9
892 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
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
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
starter POM and spring-boot-starter,  spring-boot-web 2 57
diffSum example 4 37
table example 4 29
expectj telnet failing 5 36
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 how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

803 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