The code right now is: package java; public class Java { /** * @param args the c
ID: 3532443 • Letter: T
Question
The code right now is:
package java;
public class Java {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
/**
*
* @param word
* @return
*/
public boolean isPalindrome(String wor[]) {
int left = 0; // index of leftmost unchecked char
int right = word.length() -1; // index of the rightmost
while (left < right) { // continue until they reach center
if (word.charAt(left) != word.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
}
}
}
Explanation / Answer
if u want to check whether the string is palindrome or not...here is the simplest code in java..
import java .io.*;
class palindrome
{
public static void main(String args[])throws IOException
{
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
String s1,s2="";
int i,l;
char ch;
System.out.println("ENTER STRING TO CHECK");
s1=in.readLine();
l=s1.length();
for(i=l-1;i>=0;i--)
{
ch=s1.charAt(i);
s2=ch+s2;
}
if(s1.equals(s2))
System.out.println("PALINDROME");
else
System.out.println("nOT PALINDROME");
}
}
You can modify this code for integers ...
import java.io.*;
public class Palindrome {
public static void main(String [] args){
try{
BufferedReader object = new BufferedReader(
new InputStreamReader(System.in));
System.out.println("Enter number");
int num= Integer.parseInt(object.readLine());
int n = num;
int rev=0;
System.out.println("Number: ");
System.out.println(" "+ num);
for (int i=0; i<=num; i++){
int r=num;
num=num/10;
rev=rev*10+r;
i=0;
}
System.out.println("After reversing the number: "+ " ");
System.out.println(" "+ rev);
if(n == rev){
System.out.print("Number is palindrome!");
}
else{
System.out.println("Number is not palindrome!");
}
}
catch(Exception e){
System.out.println("Out of range!");
}
}