OriginaoQuestion:WriteasubroutinenamedisPalindromewhichreturnstrueifthewordpassedasaparameterisapalindromee.g.madamisapalindrome.Writeamainprogramwhichasksausertoenterawo...
Originao Question:Write a subroutine named isPalindrome which returns true if the word passed as a parameter is a palindrome e.g. madam is a palindrome. Write a main program which asks a user to enter a word and prints on the screen if the word is a palindrome or not. The program keeps on asking words until the user presses only Enter.
Hint! If the user presses only Enter the length of the word is 0.
My solution here:
import java.util.Scanner;
public class task5{
public static int isPalindrome(String word){
int length=word.length();
int half=0; //use the half to boolean palindrome
int place=0,place1,place2; //
int i=1;
int cases=length%2;//cases, two cases, one is the length is even and other one is the odd
if(cases==1){ //when it is odd
half=length/2; //eg 5/2=3 becasue of int
place1=half-1-i;
place2=half-1+i;
char textplace1=word.charAt(place1);
char textplace2=word.charAt(place2);
for(i=1;place=0 && textplace1=textplace2;i++){
}
if(place1==0 && textplace1==textplace2) {
System.out.println("It is palindrome");
}
else{
System.out.println("It is not palindrome");
}
}
}
public static void main(String args[]){
Scanner stdin = new Scanner(System.in);
System.out.println("Please enter a word");
String word=stdin.nextLine();
isPalindrome(word);
}
}
Here I just make one case for when it is the even.
---------- Capture Output ----------
> "C:\Program Files\Java\jdk1.6.0_14\bin\javac.exe" task5.java
task5.java:17: operator && cannot be applied to int,char
for(i=1;place=0 && textplace1=textplace2;i++){
^
task5.java:17: incompatible types
found : int
required: boolean
for(i=1;place=0 && textplace1=textplace2;i++){
^
2 errors
> Terminated with exit code 1.
Could you tell me why it is not correct?
展开