#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2008
    Posts
    1
    Rep Power
    0

    Talking Java codes for a palindrome program


    i need to get the program to run properly. The following program has a small problem. Every time i used the term "strangers" as my output answer the answer reads "its a palindrome". Obviously, this testing went amiss because the term "strangers" does not read the same backward as forward. I think the error starts here:"if (wword[i] == wword[j]) {". How can i fix the problem without employing booleans?

    import java.io.Console;

    class palindrome {

    public static void main(String[] args) {
    Console InStream = System.console();
    String word = InStream.readLine("Provide one word.");
    char wword[] = word.toCharArray();
    for (int i = 0, j = (wword.length - 1); i < (wword.length / 2); i++, j--) {
    if (wword[i] == wword[j]) {
    System.out.println("it's a palindrome!");
    return;
    }
    }
    System.out.println("It's not a palindrome!");

    }
    }

    Any help is greatly appreciated. thank u
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2006
    Location
    New Jersey
    Posts
    219
    Rep Power
    137
    You're on the right track. "Strangers" is a palindrome (according to your program) because it saw it had an "s" on the front and back of the word.

    Currently, you are assuming that the word is not a palindrome, then if you find two letters that match, you assume it is a palindrome. Try reversing the algorithm. Assume that the word is a palindrome, then if you find two letters that don't match, it's not.

    Comments on this post

    • Yawmark agrees
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2008
    Posts
    14
    Rep Power
    0
    Originally Posted by koobklyn
    How can i fix the problem without employing booleans?
    Here is a code that works...
    Code:
    import java.util.*;
    class Palindrome
    {
    	public static void main(String[] args)
    	{
    		Scanner inScanner = new Scanner(System.in);
    		System.out.print("Provide one word: ");
    		String word = inScanner.nextLine();
    		char array[] = word.toCharArray();
    		for (int i = 0, j = (array.length - 1); i < (array.length / 2); i++, j--)
    		{
    			if (array[i] != array[j])
    			{
    				System.out.println(word + "is not a palindrome!");
    				break;
    			}
    			if (i == array.length / 2 - 1)
    			{
    				System.out.println(word + " is a palindrome!");
    			}
    		}
    	}
    }
    like destin said, you should start by assuming that it is a palindrome. when i reaches the middle (array.length / 2 - 1), you know it's a palindrome.
  6. #4
  7. Daniel Schildsky
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Mar 2004
    Location
    KL, Malaysia.
    Posts
    1,555
    Rep Power
    1621

    Is there a faster way?


    I think there should be a way with less written codes but slower in performance:

    1. get substring of (length/2) - 1 characters from original string.
    2. reverse the substring and append to the version of substring before it was reversed to form a complete palindrome.
    3. compare the newly formed palindrome with original string.
    When the programming world turns decent, the real world will turn upside down.

IMN logo majestic logo threadwatch logo seochat tools logo