Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Palindrome Stacks

  1. #1
    Junior Member
    Join Date
    Oct 2009
    Posts
    26
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Palindrome Stacks

    I am doing a program on palindrome using only stacks
    my code works the only question I have is how can i run my program so that it ignores spaces and non letter symbols??
    //importing packages needed to create class
    import java.util.*;
    import java.io.*;
    //creating class which will call all methods from stacks and queues
    public class StacksAndQueues
    {
        //creating main method where methods will be called
        public static void main(String[] args)throws Exception
        {
            //initializing variables
            String input;
            String lower;
            String reversed = "";
            //creating scanner which will store the users input
            Scanner console = new Scanner(System.in);
            System.out.println("Please enter a String which you think is a palindrome");
            input = console.next();
            lower = input.toLowerCase();
     
            LinkedStackClass<Character> stringStack = new LinkedStackClass<Character>();
     
            for(char c: lower.toCharArray())
            {
                stringStack.push(c);
            }
            while(!stringStack.isEmptyStack())
            {
               reversed +=  stringStack.pop();
            }
     
            System.out.println(reversed);
            if(lower.equals(reversed))
                System.out.println(" The String entered IS a Palindrome!!!");
            else
                System.out.println(" The String entered IS NOT a Palindrome!!!");
            System.out.println(input);
            System.out.println(lower);
     
        }
    }


  2. #2
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Palindrome Stacks

    for(char c: lower.toCharArray())
            {
    	if(Character.isLetter(c))
    	         stringStack.push(c);
            }

    Regards,
    Chris

Similar Threads

  1. Check for palindrome numbers
    By SnooSnoo in forum What's Wrong With My Code?
    Replies: 7
    Last Post: March 4th, 2010, 06:11 PM