Solved

How to reverse words using a stack

Posted on 2004-04-19
3
614 Views
Last Modified: 2010-08-05
I am a graduate student taking a second level C# course.  This is homework....so don't get snippy trying to sniff me out  :)

2 text boxes..and 1 button.

The click event on button1 should read whatever word (or number) is typed in textbox1 and output to  textbox2 whether or not the input in textbox1 is a palindrome.  (ie "level" in textbox1, when button1 is clicked, textbox2 will read "level is a palindrome"...whereas "lever" in textbox1, when button1 is clicked, textbox2 will read "lever is NOT a palindrome".)  This needs to work for words or numbers.  I've done stuff like this before in C++ using various functions, but am unclear how to do this in c# using a stack.

The core dificulty for me lies in the not knowing how to separate the characters within the stack.  Its simple enough to pop off words in reverse order (ie push green, push red.....pop red, pop green), but how do I do that with the actual characters?

Any thoughts would be greatly appreciated.

Shawna

0
Comment
Question by:smccloud
  • 2
3 Comments
 
LVL 5

Expert Comment

by:tgannetts
ID: 10858690
You can use the Arraylist to store the characters held in the text field of the textbox, and then the reverse method to reverse the order of the letters.

So a possible function to check for a palindrome could be:

string reversed = "";
                  
//Pass characters to arraylist
ArrayList al = new ArrayList(this.textBox1.Text.ToCharArray());

//Sort Arraylist
al.Reverse();

//Pass arraylist to string
foreach(char letter in al)
{
     reversed += letter;
}

if(reversed.ToUpper() == this.textBox1.Text.ToUpper())
{
     this.textBox2.Text = this.textBox1.Text.ToString() + " is a palindrome";
}
else
{
     this.textBox2.Text = this.textBox1.Text.ToString() + " is not a palindrome";
}

You would need to ignore spaces if you are trying to check if sentences are palindromes, as any string comparision would fail if the spaces don't match.

Hope this points you in the right direction for your homework!

Tom.
0
 
LVL 5

Accepted Solution

by:
tgannetts earned 70 total points
ID: 10858858
Another option is to use the Stack class held in System.Collections to create a First-In-Last-Out method:

string reversed = "";
                  
Stack test;
                  
test = new Stack(this.textBox1.Text.ToCharArray());

while(test.Count > 0)
{
     reversed += test.Pop();
}

...

Tom.
0
 

Author Comment

by:smccloud
ID: 10859104
Thanks Tom!!!

dekroW ekil a mrahc     :)
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
Why don't I see this table in EDMX file? 2 23
Events in static methods 3 50
Need to pass a connection through class/Method 1 39
Entity Framework 7 31
Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

910 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

21 Experts available now in Live!

Get 1:1 Help Now