Log in

View Full Version : Ask java questions live free


wogboy95
Sep 6, 2012, 07:29 PM
I am having trouble with this question "Design and write an algorithm that will read a single word from the keyboard and prints to the screen whether this word is a palindrome or not. A palindrome is a word; (or sentence, but for this question we are only dealing with a single word); that is spelt the same way forwards as backwards. The program should then ask for another word and end if the word typed in is ‘STOP’.

Please note that this will need to be done without arrays, as it is your ability to manipulate and compare characters that is being tested."

All my attempts have been wrong

Can you please help?

jsblume
Sep 28, 2012, 10:19 AM
Reading a single word? First, I presume you are using the System.in.read() method. I haven't used it in a while, but usually this finishes the input capture when the enter key is pressed. Otherwise, you would have to test each character pressed until you got one that is not in the alphabet.

Are you using a recursive process or not?

For a non-recusive process without treating the word as an array (which it is anyway), you would use a for loop. My syntax may be buggy because I'm writing this here and not in a compiler.

boolean isPalindrome = true;
for (int I=0; I<word.length/2; I++) {
if (word.substring(I,I) != word.substring(word.length-i-1) {
isPalindrome = false;
break;
}
}