Using Stack to Reverse a Palindrome

Page 1 of 1

11 Replies - 27137 Views - Last Post: 20 September 2007 - 06:51 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=33604&amp;s=ca71e01d782d9153272980368ad461bf&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 tinyjaymz

Reputation: 0
• Posts: 7
• Joined: 19-September 07

Using Stack to Reverse a Palindrome

Posted 19 September 2007 - 02:03 PM

Here is my code:
```package edu.uhcl.sce.palindrome;
import java.util.Stack;

public class Reverse {

private Stack<Character> st;

public String reverseString (String s1) {
StringBuilder sb = new StringBuilder ();
for (int i=0; i<s1.length(); ++i){
st.push (s1.charAt(i));
}
while (!st.empty()){
char c = st.pop();
sb.append(c);
}
return sb.toString();
}
public Reverse() {

}

}

```

My assignment is to write a simple Java program that uses a java.util.Stack<E> object to test whether a phrase is a palindrome.

We were given two different algorithms to implement into our program, and the previous code was the first one, to be used in the test program.

I'm having trouble converting the second algorithm, which is:

The algorithm for your Main class:

ALGORITHM main()
Get the string s1
Remove all spaces and punctuation from s1
Print s1
Reverse s1
Print the reversed string
if s1.equals(reversed)
Print "is palindrome"
else
Print "is not palindrome"

I know how to do the print statements, using System.out.println(), but the rest, especially the removing spaces, geting the string, and reversing the string, is a bit confusing to me and the book has been no help.

I am using the NetBeans IDE, fyi.

Any help would be great!

Thanks,
Tiny

Is This A Good Question/Topic? 0

Replies To: Using Stack to Reverse a Palindrome

#2 tinyjaymz

Reputation: 0
• Posts: 7
• Joined: 19-September 07

Re: Using Stack to Reverse a Palindrome

Posted 19 September 2007 - 04:15 PM

Here is what I have for the 2nd 'Main' algorithm so far...please tell me if I am on the right track.

```package edu.uhcl.sce.palindrome;
import java.util.Scanner;

public class Main {

String s1;

public static void main(String[] args) {
Scanner input = new Scanner ( System.in );

Reverse s1 = new Reverse ();

System.out.print ("Enter a Palindrome: ");
```

#3 William_Wilson

• lost in compilation

Reputation: 207
• Posts: 4,812
• Joined: 23-December 05

Re: Using Stack to Reverse a Palindrome

Posted 19 September 2007 - 04:42 PM

i didn't run it, but your reverse method looks good, does it properly reverse a string?
If so simply compare it to the original with the spaces removed as well, if they match voila a palidrome

I don't see the part for getting user input, but i imagine you just haven't finished that yet.

#4 tinyjaymz

Reputation: 0
• Posts: 7
• Joined: 19-September 07

Re: Using Stack to Reverse a Palindrome

Posted 19 September 2007 - 06:02 PM

William_Wilson, on 19 Sep, 2007 - 04:42 PM, said:

I don't see the part for getting user input, but i imagine you just haven't finished that yet.

What would be the best way to get input...doing something like s1=next.String...or am I way off?

#5 William_Wilson

• lost in compilation

Reputation: 207
• Posts: 4,812
• Joined: 23-December 05

Re: Using Stack to Reverse a Palindrome

Posted 19 September 2007 - 06:29 PM

Amadeus (2nd post) gives a good example for an integer, you can adapt this for Strings.

#6 tinyjaymz

Reputation: 0
• Posts: 7
• Joined: 19-September 07

Re: Using Stack to Reverse a Palindrome

Posted 19 September 2007 - 10:45 PM

Ok, here is my final code:

```package edu.uhcl.sce.palindrome;
import java.util.Stack;
import java.util.Scanner;

public class Main {

public static void main(String[ ] args)
{
Scanner stdin = new Scanner(System.in);
String s1;

do
{
System.out.println("Enter an expression: ");
s1 = stdin.nextLine( );
if (reverseString(s1))
System.out.println("is palindrome.");
else
System.out.println("is not palindrome.");
}
while (s1.length( ) != 0);
}

public static String reverseString(String input)
{
Stack<Character> st = new Stack<Character>( );
String s1;

StringBuilder sb = new StringBuilder ();
for (int i=0; i<s1.length(); ++i){
st.push (s1.charAt(i));
}
while (!st.empty()){
char c = st.pop();
sb.append(c);
}
return sb.toString();
}

}
```

Thanks a bunch for the help...I am having just one more problem:

In the line of code under the first do statement, the line: if (reverseString(s1)) is giving me a problem. In NetBeans, that line has an error that says incompatible types, found java.lang.String, required: boolean This is completely stumping me...can anyone see what i have done wrong?

Thank you.

#7 William_Wilson

• lost in compilation

Reputation: 207
• Posts: 4,812
• Joined: 23-December 05

Re: Using Stack to Reverse a Palindrome

Posted 20 September 2007 - 05:27 AM

sure, no problem, this seems to be a common issue when learning any language

public static String reverseString(String input)
has a return ^ value ^ of String, but an if statement evaluates booleans, if(true) or if(false).
you are looking for something along the lines of:
if ((reverseString(s1)).equals(s1))
this compares the 2 Strings (properly) and returns a boolean as to whether they are equal.

One other thing, i noticed:
```public static String reverseString(String input)
{
Stack<Character> st = new Stack<Character>( );
String s1;

StringBuilder sb = new StringBuilder ();
for (int i=0; i<s1.length(); ++i){
st.push (s1.charAt(i));
}
while (!st.empty()){
char c = st.pop();
sb.append(c);
}
return sb.toString();
}

}

```

you create a variable s1 in this method, but you do not initialize it. It is also determining the for loop's run time, which will by none, since s1.length = 0
I assume this is just a mistake from changing variable names, and you are using it as if it were in the scope of your main method, s1 does nothing in the method, so it should be more like this:
```public static String reverseString(String input)
{
Stack<Character> st = new Stack<Character>( );
//removed s1 creation

StringBuilder sb = new StringBuilder ();
for (int i=0; i<input.length(); ++i){  //replaced s1 for input
st.push (input.charAt(i));  //replaced s1 for input
}
while (!st.empty()){
char c = st.pop();
sb.append(c);
}
return sb.toString();
}

}

```

feel free to ask more questions about clarification, or brand new ones

#8 tinyjaymz

Reputation: 0
• Posts: 7
• Joined: 19-September 07

Re: Using Stack to Reverse a Palindrome

Posted 20 September 2007 - 10:30 AM

Thanks, I really appreciate it...it bulids fine. However, apparently I am doing another thing wrong, because it isnt doing exactly what I need it to do. I need it to read the string, print the inputed string (for which i have added a print statement, then remove the punctuation and spaces, make it all caps, and then print the reversed string with no punct., spaces, and in all caps. Do I have the right code and just not initializing it correctly, or do I need to at another method, such as an isLetter function....any suggestions?

Thanks.

#9 William_Wilson

• lost in compilation

Reputation: 207
• Posts: 4,812
• Joined: 23-December 05

Re: Using Stack to Reverse a Palindrome

Posted 20 September 2007 - 11:46 AM

can you post the complete code you are using?

Have you made sure that the punctuation are being removed properly as well as the switch to capitals?

#10 tinyjaymz

Reputation: 0
• Posts: 7
• Joined: 19-September 07

Re: Using Stack to Reverse a Palindrome

Posted 20 September 2007 - 12:02 PM

William_Wilson, on 20 Sep, 2007 - 11:46 AM, said:

can you post the complete code you are using?

Have you made sure that the punctuation are being removed properly as well as the switch to capitals?

```/*
* Main.java
*
* Created by James Spruiell
*/
package edu.uhcl.sce.palindrome;
import java.util.Stack;
import java.util.Scanner;

public class Main {

public static void main(String[ ] args)
{
Scanner stdin = new Scanner(System.in);
String s1;

do
{
System.out.println("Enter an expression: ");
s1 = stdin.nextLine( );
System.out.println(s1);
if ((reverseString(s1)).equals(s1))
System.out.println("is palindrome.");
else
System.out.println("is not palindrome.");

}
while (s1.length( ) != 0);
}

public static String reverseString(String s1)
{
Stack<Character> st = new Stack<Character>( );

StringBuffer sb = new StringBuffer ();
for (int i=0; i<s1.length() - 1; ++i){
st.push (s1.charAt(i));
}
while (!st.empty())
{
char c = st.pop();
sb.append(c);
}
return sb.toString();
}

}

```

The punctuation isnt being removed, and it isnt being switch to all caps...the only results i get is what I input.

#11 William_Wilson

• lost in compilation

Reputation: 207
• Posts: 4,812
• Joined: 23-December 05

Re: Using Stack to Reverse a Palindrome

Posted 20 September 2007 - 12:22 PM

to get all capitals:
s1 = s1.toUpperCase();

the punctuation will require a loop or split depending how you go about it, take a shot at it, and i can help you with it.

#12 tinyjaymz

Reputation: 0
• Posts: 7
• Joined: 19-September 07

Re: Using Stack to Reverse a Palindrome

Posted 20 September 2007 - 06:51 PM

This part is turning out to be hard for me....

I was thinking it would be another for loop, but using "variable name" = s1.charAt(i); instead of the stack...
but have no idea where I would put that..would i create another public static method, or right in the main method.

This post has been edited by tinyjaymz: 20 September 2007 - 06:52 PM