Stack question

L

LedZep

What up everyone,

I have to write a program that uses a stack to determine whether a
string is a palindrome (a string that is spelled identically backward
and forward). The program has to ignore spaces, case sensitivity and
punctuation.
I need three text boxes. The first will be for input of the string.
The second will display the string when the "check" button is clicked
and the result of the analysis (string is a palindrome or is not a
palindrome). The third text box should show how the two strings
compared, with the first one being displayed forward and the second
backward.
I have to use a stack for this problem and I'm kind of lost. If anyone
can get me started or show me some examples, that would be awesome.

TIA,
LedZep
 
S

Sergey Poberezovskiy

You may not necessarily have to use a stack.
Let's say in VB it may look like:

Dim myTextArray() As Char =
TextBox1.Text.ToCharArray
Array.Reverse(myTextArray)
Dim reverseString As String
With New System.Text.StringBuilder
.Append(myTextArray)
reverseString = .ToString
End With

Hope that is enough for you to start...
 
T

Tom Leylan

LedZep said:
I have to write a program that uses a stack to determine whether a
string is a palindrome (a string that is spelled identically backward
and forward). The program has to ignore spaces, case sensitivity and
punctuation.

It sounds surprisingly like a homework assignment :)
I have to use a stack for this problem and I'm kind of lost. If anyone
can get me started or show me some examples, that would be awesome.

You might not go for it but would you post what you have so far or ask a
specific question? Do you know what a stack is and how it works? Have you
been able to push anything onto a stack and pop it off again? I don't think
we quite know where you are stuck.
 
T

Tom Leylan

I think the homework assignment demands that he use a stack so he can get an
understanding of how a stack works. We might let him try first.
 
J

Jay B. Harlow [MVP - Outlook]

LedZep,
Have you looked at the System.Collections.Stack class?

It has methods to Push & Pop an item onto the stack, it also has a method
Peek to check the top item on the stack.

I would not expect the assignment to be that you need to write the stack
class itself.

Hope this helps
Jay
 
L

LedZep

So what if its a homework assignment?? My teacher sucks and the book
sucks. How else am I supposed to learn, the .NET help menus? Please.
If anyone wants to help me, awesome, I thought thats what these
newsgroups were supposed to be all about. If not fine.

LedZep
 
L

LedZep

Basically I dont know what a stack is or why I have to use it. Why
cant I just do it how Sergey says. I dont want someone to write the
entire code and send it to me. I just want to understand what the hell
I'm doing and why. Thats the problem with my teacher - he does the
complete opposite... which is nothing.

Thanks for any replies...

LedZep
 
J

Jay B. Harlow [MVP - Outlook]

LedZep,
It sounds like you realize there is a difference of someone doing the
assignment for you, which is cheating. And you asking in the newsgroup what
a stack is and how it works. Some students don't realize this or care about
this, they simple want the assignment done, hence my and Tom's caution.

However the assignment is designed to get you to understand how a stack
works, so answering the question is one of those gray areas when it could be
considered cheating.

I also hope you realize that if your teacher is truly as bad as you make him
sound and other students feel this way, that is a matter for the
administration of the school.

Any way, what is a stack:

http://abstractvb.com/code.asp?A=1023

A stack is an object that stores other items (objects), where these other
items can be retrieved in a LIFO (Last In, First Out) manner. To put items
on the stack you use stack.Push(item) to remove items from the stack you use
item = stack.Pop, where stack is your Stack variable.

Think of a stack as the dispenser for trays or plates at a cafeteria. When
the kitchen help puts more trays on the top of the dispenser the old ones
are pushed down, when you pick up a tray to get your food you get the last
one put on the dispenser, its popped off the top.

If you think of how the stack works last in, first out, think how that could
be used to reverse the order of a string.

If you take the first character of a string and put it on the stack, if you
take the next character of a string and put it on the stack...

If you pop a character off the stack, and put it on the end of a new string,
if you pop another character off the stack and put it on the end of a new
string...

Hope this helps
Jay
 
L

LedZep

Jay B. Harlow said:
LedZep,
It sounds like you realize there is a difference of someone doing the
assignment for you, which is cheating. And you asking in the newsgroup what
a stack is and how it works. Some students don't realize this or care about
this, they simple want the assignment done, hence my and Tom's caution.

However the assignment is designed to get you to understand how a stack
works, so answering the question is one of those gray areas when it could be
considered cheating.

I also hope you realize that if your teacher is truly as bad as you make him
sound and other students feel this way, that is a matter for the
administration of the school.

Any way, what is a stack:

http://abstractvb.com/code.asp?A=1023

A stack is an object that stores other items (objects), where these other
items can be retrieved in a LIFO (Last In, First Out) manner. To put items
on the stack you use stack.Push(item) to remove items from the stack you use
item = stack.Pop, where stack is your Stack variable.

Think of a stack as the dispenser for trays or plates at a cafeteria. When
the kitchen help puts more trays on the top of the dispenser the old ones
are pushed down, when you pick up a tray to get your food you get the last
one put on the dispenser, its popped off the top.

If you think of how the stack works last in, first out, think how that could
be used to reverse the order of a string.

If you take the first character of a string and put it on the stack, if you
take the next character of a string and put it on the stack...

If you pop a character off the stack, and put it on the end of a new string,
if you pop another character off the stack and put it on the end of a new
string...

Hope this helps
Jay

All right,

Here's my code. Two questions: 1.) Why doesnt the enumerator display
the the item(s) pushed onto the stack in txtOutput??? When I have a
separate button to push an item onto a stack
(stack.Push(txtInput.Text))and then another button to display the
stack:

While enumerator.MoveNext()
buffer.Append(enumerator.Current)
txtOutput.Text = buffer.ToString
End While

it works fine. Why cant I do both push and display in the same button?

2.) Any suggestions on how to push "word" onto the stack letter by
letter? I tried using a loop where

count = word.Length

Do while i =< count
stack.Push(word.Chars(n))
n = n + 1
i = i + 1
Loop


This kinda works because it displays the stack LIFO-esque (the word
appears backwards when displayed). So then how would I display "word"
in non-LIFO (so that "word" appears correctly spelled)? Is Chars the
right way to enter "word" letter by letter? Should I make it an
array??? Kinda confused....

Dim stack As New stack()
Dim word As String

Private Sub cmdDisplay_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdDisplay.Click
Dim enumerator As IEnumerator = stack.GetEnumerator
Dim buffer As StringBuilder = New StringBuilder()
Dim count As Integer
word = txtInput.Text
count = word.Length
stack.Push(word)

While enumerator.MoveNext()
buffer.Append(enumerator.Current)
txtOutput.Text = buffer.ToString
End While

End Sub

Any help is MUCH APPRECIATED,

LedZep
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top