Solved

reverse a word using stack

Posted on 2003-11-06
9
883 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
create a gui in perl 3 72
eclipse package explorer vs project explorer view 2 84
Strange loading of website behaviour 3 32
nextBoolean(double p) for Random class 3 35
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
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…

920 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

15 Experts available now in Live!

Get 1:1 Help Now