import java.util.Scanner;
public class Palindrome {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Please enter a string: ");
String s = input.next();
char [] palindrome = s.toCharArray();
if (isPalindrome(palindrome)) {
System.out.println(s + " is a palindrome.");
} else {
System.out.println(s + " is not a palindrome. ");
}
}
public static boolean isPalindrome(char[] palindrome) {
char[] result = new char[palindrome.length];
for (int i = 0, j = palindrome.length - 1; i < palindrome.length; i++, j--) {
result[j] = palindrome[i];
}
if (palindrome == result) {
return true;
}
return false;
}
}
Java Program to Identify Palindromes
Page 1 of 15 Replies - 237 Views - Last Post: 29 October 2012 - 07:50 PM
#1
Java Program to Identify Palindromes
Posted 29 October 2012 - 06:57 PM
Hi everyone! I amy trying to write a program to identify whether a string that has been inputted by the user is a palindrome or not but my output is wrong /: Here is my code! If somebody could help me with what's wrong it would be much appreciated (:
Replies To: Java Program to Identify Palindromes
#2
Re: Java Program to Identify Palindromes
Posted 29 October 2012 - 07:18 PM
You can't compare arrays like that
if (palindrome == result) {
to test if a String is a plaindrome just compare if
digit[0] == digit[digit.length-1]
digit[1] == digit[digit.length-2]
digit[2] == digit[digit.length-3]
up to
digit[digit.length / 2]
if (palindrome == result) {
to test if a String is a plaindrome just compare if
digit[0] == digit[digit.length-1]
digit[1] == digit[digit.length-2]
digit[2] == digit[digit.length-3]
up to
digit[digit.length / 2]
#3
Re: Java Program to Identify Palindromes
Posted 29 October 2012 - 07:24 PM
The problem is you are not comparing the values of the two arrays. Let's take a look at an example.
When you compare one array to another using == it does not compare the values in the array.
EDIT: Darn you pbl you beat me to it.
char[] set1 = {'a', 'b', 'c'};
char[] set2 = {'a', 'b', 'c'};
System.out.println(set1 == set2); //prints false
When you compare one array to another using == it does not compare the values in the array.
EDIT: Darn you pbl you beat me to it.
This post has been edited by Kinaces: 29 October 2012 - 07:24 PM
#5
Re: Java Program to Identify Palindromes
Posted 29 October 2012 - 07:49 PM
That should do it
boolean isPalindrome(char[] digit) {
int last = digit.length - 1;
for(int i = 0; i < digit.length/2; ++i) {
if(digit[i] != digit[last])
return false;
--last;
}
return true;
}
#6
Re: Java Program to Identify Palindromes
Posted 29 October 2012 - 07:50 PM
You're being way too complicated. If you just want to check only single words like mom, dad, pop, racecar, etc, you can just do:
If you want to check for phrases and sentences, it becomes a little bit more complicated, because you have to deal with spacing, but all you need to do is build a one string from input with no spaces (String.split()), then another with no spaces reversed, then check their equality.
public boolean isPalindrome(String s) {
String input = s.toLowerCase();
String reversed = new StringBuffer(input).reverse().toString();
return input.equals(reversed);
}
If you want to check for phrases and sentences, it becomes a little bit more complicated, because you have to deal with spacing, but all you need to do is build a one string from input with no spaces (String.split()), then another with no spaces reversed, then check their equality.
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote




|