
/*
Online Java - IDE, Code Editor, Compiler

Online Java is a quick and easy tool that helps you to build, compile, test your programs online.
*/

public class Main
{
    
    public static void main(String[] args) {
    System.out.println("Welcome to Online IDE!! Happy Coding :)");
        
        // the example is failing on teh first test  such as   aka?   or   ?aka
        
        //System.out.println(isPalindrome("a#kab?"));
        
        //String sample = "Maneuquenam";
        //String sample = "Eva, *&can I see bees in a#* cave?";
        //String sample = "Eva, can I see bees in a cave?";
        //String sample = "#le?$vel";
        //String sample = "This phrase, surely, is not a palindrome!";
        //String sample ="a";
        
        System.out.println("INITIAL STRING: " + sample);
        
        String sampleLowerCase = sample.toLowerCase();  //converted entire string lowercase to ensure no ambiguity in comparison..
        
        System.out.println(isPalindrome(sampleLowerCase));   // method call
        
    }
    
    public static String isPalindrome(String str)
    {
        
        boolean specialCharFront=false;
        boolean specialCharBack=false;
        
        //Special characters need to be excluded from checking....
        //'\'   unable to process these in special characters since it recognises it as escape character
        
        Character [] special = new Character[] {' ', ',', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '-', '_', '=', '+', '|', '[', ']', '{', '}', ';', ':', '/', '?', '.', '>'};
        
        
        
        while (str.length()>1)   // when there is one character left
        {
            
            for (int j=0; j<special.length; j++)
            {
                if (str.charAt(0)==special[j])
                {
                    specialCharFront=true;
                    System.out.println("special char front");
                    System.out.println("analysis on here:" + str.substring(1,( str.length())));
                    
                     //return isPalindrome(str.substring(1,( str.length()-1)));
                }
                
                 if (str.charAt(str.length()-1)==special[j])
                {
                    specialCharBack=true;
                    System.out.println("special char end");
                    
                    System.out.println("analysis on here:" + str.substring(0,( str.length()-1)));
                    
                    
                     //return isPalindrome(str.substring(1,( str.length()-1)));
                }
            }
            
            if(specialCharFront && specialCharBack)  // if special character in both front and back, it shortens string
            // by 1 front and back
            {
                
                return isPalindrome(str.substring(1,( str.length()-1)));
                // this is fine since it truncates one character front
                // it also needs to remove char from end... Since its zero index last char is  str.length()-1
                // using str.length()-1 is the exclusion, so it will take two chars before.
                
            }
            
             if(!specialCharFront &&  specialCharBack)  //if special character back only 
            {
                
                return isPalindrome(str.substring(0,( str.length()-1)));
                // it will start from front..... 
                // this is fine since it retains characters front
                // it also needs to remove char from end... Since its zero index last char is  str.length()-1
                // using str.length()-1 is the exclusion, so it will take two chars before.
               
            }
            
             if(specialCharFront &&  !specialCharBack)  //if special character front and none back
            {
                System.out.println("string at this point:" + str.substring(1,( str.length())));
                
                
                return isPalindrome(str.substring(1,( str.length())));
                // this is fine since it removes one character front
                // it also needs to keep char from end... Since its zero index last char is  str.length()-1
                // Since last character is required, it will be set to str.length parameter
                
            }
            
        if (str.charAt(0)==str.charAt((str.length()-1)))
        {
            System.out.println("compare1:" + str.charAt(0));
            System.out.println("compare2:" + str.charAt(str.length()-1));
            
            //it needs to truncate string by one character front and back.........
            System.out.println("analysis on here:" + str.substring(1,( str.length()-1)));
            
            return isPalindrome(str.substring(1,( str.length()-1)));
        }
    
        else
        {
            System.out.println(str.charAt(0) + " does not equal: " + str.charAt(str.length()-1));
            return "\nfalse";
        }
        }
        
        return "\ntrue";
        
    }
    
}