Monday, 11 July 2011

To find given string is palindrom or not using stack in java

Write a function to test if a string is a palindrome using a stack. You can push characters in the stack one by one. When you reach the end of the string, you can pop the characters and form a new string. If the two strings are exactly the same, the string is a palindrome. Note: Palindrome ignores spacing, punctuation, and capitalization. Test your program with the following test cases.
  1. Go Dog
  2. Madam, I'm Adam
  3. Madam, I'm not a palindrome 

Here I am creating 3 files       1. StackADT.java       2. JCStack.java            3. Palindrome.java

// 1.  StackADT.java

package Stack;
public interface StackADT
{
    public int size();
    public void push(char element);
    public String pop();


}


// 2.  2. JCStack.java  

 import Stack.*;
import java.io.*;
import java.util.*;
import java.lang.*;
public class JCStack implements StackADT
{
    Stack st=new Stack();
    int top=-1;
  
    public int size()
    {
        return st.size();
    }
    public boolean isEmpty()
    {
        return st.isEmpty();
    }
  
    public void push(char element)
    {
        Character c=element;
        try
        {
            st.push(new Character(element));
            top++;
        }
        catch (Exception e)
        {
        }
  
    }
    public String pop()
    {
        String str="";
        if(st.size()==0)
        {
            System.out.println("\nSorry stack is empty");
            System.exit(0);
          
        }
        else
        {

        int i=st.size();
        for (int j=0;j<i;j++ )
        {
         Character ob=ob=(Character)st.pop();
         str=str+ob.charValue();
        }
        }
      
        return str;
          
      
  
      
    }
    public void exit()
    {
        System.exit(0);
    }

}


3. 3. Palindrome.java

import java.util.Scanner;
import java.lang.Character;
class Palindrome
{
    public static void main(String[] args)
    {
                        JCStack ST=new JCStack();
                        Scanner sc=new Scanner(System.in);
                        int choice;
                        String el,el1;
                        String st="";
                        System.out.println("Please enter Enter you new string line");
                       
                        el1=sc.nextLine();

                        el1=el1.replaceAll(" ", ""); // removning places in string
                        el1=el1.replaceAll(",","");
                        el1=el1.replaceAll("'", "");
                       
                        el=el1.toLowerCase();      // converting all the characters to lower case
                       

                        while (true)
                        {
                            System.out.println("\nPlease enter you choice\n");
                            System.out.println(" 1 to insert or push \n 2 to check given string is palindrom");
                            System.out.println(" 3 to exit \n");
                            choice=sc.nextInt();
                            switch (choice)
                            {
                            case 1:

                               
                                int k=el.length();
                                for(int i=0;i<k;i++)
                                {
                                    char ch=el.charAt(i);
                                           
                                     ST.push(ch);   
                                                                  
                                   
                                }
                                break;       
                            case 2:
                               
                               
                                if(ST.size()!=0)
                                {
                                    String str=ST.pop();
                               
                                    if(el.equals(str))
                                    {

                                        System.out.println("\nString is palindrom\n");
                                    }
                                    else
                                    {
                                        System.out.println("\nString is not palindrom\n");
                                    }
                                }

                               
                                break;
                            case 3:
                                ST.exit();
                            default:
                                System.out.println("Plese choose correct choice");
                               
                           
                            }
                                   

                        }
       
    }
}