1public static boolean checkPalindrome(String str)
2{
3
4 int len = str.length();
5
6 for(int i = 0; i < len / 2; i++) {
7 if (str.charAt(i) != str.charAt(len - i - 1))
8 return false;
9 }
10 return true;
11}
1import java.util.*;
2class PalindromeExample2
3{
4 public static void main(String args[])
5 {
6 String original, reverse = ""; // Objects of String class
7 Scanner in = new Scanner(System.in);
8 System.out.println("Enter a string/number to check if it is a palindrome");
9 original = in.nextLine();
10 int length = original.length();
11 for ( int i = length - 1; i >= 0; i-- )
12 reverse = reverse + original.charAt(i);
13 if (original.equals(reverse))
14 System.out.println("Entered string/number is a palindrome.");
15 else
16 System.out.println("Entered string/number isn't a palindrome.");
17 }
18}
1import java.util.Scanner;
2
3public class Palindrome
4{
5 public static void main(String args[])
6 {
7 int num,temp,reverse=0;
8 Scanner input=new Scanner(System.in);
9 num=in.nextInt();
10 temp=num;
11 //code to reverse the number
12 while(temp!=0)
13 {
14 int d=temp%10; //extracts digit at the end
15 reverse=reverse*10+d;
16 temp/=10; //removes the digit at the end
17 }
18 // 'reverse' has the reverse version of the actual input, so we check
19 if(reverse==num)
20 {
21 System.out.println("Number is palindrome");
22 }
23 else
24 {
25 System.out.println("Number is not palindrome");
26 }
27 }
28}
1package test
2//The function below checks if a string is a palindrome
3//True = Is a palindrome & False = Not a palindrome
4 public boolean isPalindromString(String text){
5 String reverse = reverse(text);
6 if(text.equals(reverse))
7 {
8 return true;
9 }
10
11 return false;
12 }
13//This function returns the reverse String of its input.
14//Ex. if given "hello", it will return "olleh"
15 public String reverse(String input)
16 {
17 if(input == null || input.isEmpty())
18 {
19 return input;
20 }
21 return input.charAt(input.length()- 1) + reverse(input.substring(0, input.length() - 1));
22 }
1package test
2//The function below checks if a string is a palindrome
3//True = Is a palindrome & False = Not a palindrome
4 public boolean isPalindromString(String text){
5 String reverse = reverse(text);
6 if(text.equals(reverse))
7 {
8 return true;
9 }
10
11 return false;
12 }
13//This function returns the reverse String of its input.
14//Ex. if given "hello", it will return "olleh"
15 public String reverse(String input)
16 {
17 if(input == null || input.isEmpty())
18 {
19 return input;
20 }
21return input.charAt(input.length()- 1) + reverse
22 (input.substring(0, input.length() - 1));
23 }
1// String palindrome in java using array
2import java.util.Arrays;
3import java.util.Scanner;
4public class StringPalindromeUsingArray
5{
6 public static void main(String[] args)
7 {
8 System.out.println("Please enter string to check whether string is palindrome: ");
9 Scanner sc = new Scanner(System.in);
10 String strInput = sc.nextLine();
11 char[] chArray = strInput.toCharArray();
12 int size = chArray.length;
13 char[] chGiven = Arrays.copyOf(chArray, chArray.length);
14 for(int a = 0; a < size / 2; a++)
15 {
16 char temp = chArray[a];
17 chArray[a] = chArray[size - a - 1];
18 chArray[size - a - 1] = temp;
19 }
20 System.out.println("Given array: " + Arrays.toString(chGiven));
21 System.out.println("String palindrome using array: " + Arrays.toString(chArray));
22 if(Arrays.equals(chArray, chGiven))
23 {
24 System.out.println("string is palindrome.");
25 }
26 else
27 {
28 System.out.println("string is not a palindrome.");
29 }
30 sc.close();
31 }
32}