Results 1 to 3 of 3

Thread: Palindrome

  1. #1
    threlot is offline Member
    Join Date
    Oct 2012
    Posts
    11
    Rep Power
    0

    Question Palindrome

    How can i fix this problem. When i type for example: "!!radar????" it says that is not a palindrome. Whenever i type different punctuation marks, it says that is not a palindrome.

    The method should ignore spaces and punctuation in the string.

    Java Code:
    import java.util.Scanner;
    public class Palindromes{
        static boolean testPalindrome(String s)
        {
            if(s.length()==0 || s.length()==1)
            {
                return true;
            }
            s=s.toLowerCase().replaceAll(" ", "");
            s = s.replace(".", "");
            if(s.charAt(0)==s.charAt(s.length()-1))
                return testPalindrome(s.substring(1, s.length()-1));
            return false;
        }
    
        public static void main(String[] args) 
        {
            Scanner sc = new Scanner(System.in);
            System.out.print("Enter String: ");
            String s = sc.nextLine();
            if(testPalindrome(s))
                System.out.println("It's a Palidrome");
            else
                System.out.println("It's not a Palindrome");
    
        }
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default Re: Palindrome

    Try doing some debugging by printing out the values of the variables as the code executes so you see what the code sees when it executes. The print out will help you understand what the code is doing.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    finest is offline Member
    Join Date
    Nov 2013
    Posts
    2
    Rep Power
    0

    Default Re: Palindrome

    your program only takes care of spaces and a period characters in the string, include other special characters as well.

Similar Threads

  1. palindrome
    By rochellelising in forum New To Java
    Replies: 1
    Last Post: 08-24-2011, 02:03 PM
  2. Palindrome Recursion
    By jk336699 in forum New To Java
    Replies: 6
    Last Post: 08-24-2011, 07:02 AM
  3. Palindrome
    By leepikamukharji in forum New To Java
    Replies: 2
    Last Post: 04-29-2011, 04:20 PM
  4. Palindrome
    By pinkdreammsss in forum Java Applets
    Replies: 8
    Last Post: 05-04-2010, 04:59 PM
  5. HELP...Palindrome
    By d7o0om in forum New To Java
    Replies: 12
    Last Post: 11-13-2009, 04:32 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •