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 4 of 4

Thread: Generation of Palindrome number in Java

  1. #1
    Junior Member
    Join Date
    May 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Generation of Palindrome number in Java

    Hi,

    I am trying to write a java program to check for palindrome...but I am not getting the correct o/p...pls help me in figuring out the prob...here is the code:

    public class Palindrome {
            public static void main(String args) {
            // TODO Auto-generated method stub
                String str = "candidate";
                Palindrome obj = new Palindrome();
                if(obj.isPalindrome(str))
                    System.out.print(" Palindrome");
                else
                    System.out.print("not Palindrome");
     
                }
     
    public boolean isPalindrome(String str) {
     
              int left = 0;             // index of leftmost unchecked char
          int right = str.length() -1; // index of the rightmost
     
        while (left < right) {       // continue until they reach center
         if (str.charAt(left) != str.charAt(right)) {
          return false;       // if chars are different, finished
        }
        left++;               // move left index toward the center
        right--;             // move right index toward the center
        }
     
    return true;             // if finished, all chars were same
        } 
    }

    public class TestforPalindrome {
     
        /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            Palindrome obj = new Palindrome();
            System.out.println ("mom is a palindrome? "+ obj.isPalindrome("mom"));
            System.out.println ("apple pie is a palindrome? "+ obj.isPalindrome("Apple Pie"));
            System.out.println ("poor dan is in a droop is a palindrome? "+ obj.isPalindrome("poor dan is in a droop"));
            System.out.println ("poor don is in a droop is a palindrome? "+ obj.isPalindrome("Poor Don is in a droop"));
            System.out.println ("Madam, I'm Adam is a palindrome? "+ obj.isPalindrome("madam Im adam"));
            System.out.println ("A man, a plan, a canal, Panama is a palindrome? "+ obj.isPalindrome("a man, a plan, a canal, panama"));
     
     
        }
     
    }
    Last edited by Deep_4; November 7th, 2012 at 11:15 PM. Reason: Please use [code] [/code] tags


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Can someone help me figure where I went wrong in the below code

    Hello tina.goyal, welcome to the Java Programming Forums.

    What is not working? I have compiled this code and it seems to be working fine.

    This is the output I'm given:

    mom is a palindrome? true
    apple pie is a palindrome? false
    poor dan is in a droop is a palindrome? false
    poor don is in a droop is a palindrome? false
    Madam, I'm Adam is a palindrome? false
    A man, a plan, a canal, Panama is a palindrome? false
    I would say this is working correctly because "mom" is a palindrome and is returning true.
    madam in the phrase 'madam Im adam' is also a palindrome. If you remove the 'Im adam' bit, madam returns true.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  3. #3
    Junior Member
    Join Date
    May 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Can someone help me figure where I went wrong in the below code

    Thanks for the reply!

    Actually all 3 strings below are palindromes but on compiling the output is false whereas it should be true.So I am just trying to figure out why is it returning false when all 3 of them are palindromes.

    Poor Dan is in a droop.
    Madam, I'm Adam.
    A man, a plan, a canal, Panama!

  4. #4
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Can someone help me figure where I went wrong in the below code

    I thought palindromes were words that can be read the same backwards. For example, nun, mom, madam..

    The program works fine with single words.

    Those phrases only become palindromes when they are split up differently than they are presented. I'm not sure if this qualifies them as palindromes.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

Similar Threads

  1. [SOLVED] Java program to prompt and display the user input number as odd or even
    By napenthia in forum Java Theory & Questions
    Replies: 11
    Last Post: April 22nd, 2009, 01:19 AM