how to check if string is a palindrome in java code example
Example 1: palindrome number in java
import java.util.Scanner;
public class Palindrome
{
public static void main(String args[])
{
int num,temp,reverse=0;
Scanner input=new Scanner(System.in);
num=in.nextInt();
temp=num;
while(temp!=0)
{
int d=temp%10;
reverse=reverse*10+d;
temp/=10;
}
if(reverse==num)
{
System.out.println("Number is palindrome");
}
else
{
System.out.println("Number is not palindrome");
}
}
}
Example 2: palindrome function java
package test
public boolean isPalindromString(String text){
String reverse = reverse(text);
if(text.equals(reverse))
{
return true;
}
return false;
}
public String reverse(String input)
{
if(input == null || input.isEmpty())
{
return input;
}
return input.charAt(input.length()- 1) + reverse(input.substring(0, input.length() - 1));
}
Example 3: palindrome in java
package test
public boolean isPalindromString(String text){
String reverse = reverse(text);
if(text.equals(reverse))
{
return true;
}
return false;
}
public String reverse(String input)
{
if(input == null || input.isEmpty())
{
return input;
}
return input.charAt(input.length()- 1) + reverse
(input.substring(0, input.length() - 1));
}