palindrome string example in java

Java Program to Check if a Given String is Palindrome

This is java basic program which asked by interviewer to Write a Java program that accepts a string and checks whether a given string is a palindrome or not.

Before you write the code let’s understand that

What is String Palindrome Program in Java?

A palindrome is a word, phrase, or sentence that reads the same backward or forward. A string is said to be a palindromic string when we traverse it from start to end or end to start then we get the same result.

Examples:

ada, malayalam, racecar, mom, etc.

Example 1:

Input: String = “RACECAR”
Output: racecar is a Palindrome
Explanation: The reverse of the given string is equal to the (RACECAR) is equal to the given string. Therefore, the given string is a palindromic string.

Example 2:

Input: String = “SUNDAY”
Output: SUNDAY is not a Palindrome
Explanation: The reverse of the given string is equal to the (YADNUS) which is not equal to the given string. Therefore, the given string is not a palindromic string.

Example 3:

Input: String = “RADAR”
Output: RADAR is a Palindrome
Explanation: The reverse of the given string is equal to the (RADAR) which is equal to the given string. Therefore, the given string is a palindromic string.

Problem Solution:

1. Take a string as input.
2. Reverse the string.
3. If they are equal, print the string is a Palindrome else print, the string is not a palindrome.

Find the Palindrome using user input for loop in java

Method 1
/*
 * Author: Zameer Ali
 * */
import java.util.Scanner;

public class Palindrome
{
    public static void main(String args[])
    {
        String a, b = "";
        Scanner s = new Scanner(System.in);
        System.out.print("Enter a string:");
        a = s.nextLine();
        int n = a.length();
        for(int i = n - 1; i >= 0; i--)
        {
            b = b + a.charAt(i);
        }
        if(a.equalsIgnoreCase(b))
        {
            System.out.println(a+ " is a palindrome.");
        }
        else
        {
            System.out.println(a+ " is not a palindrome.");
        }
    }
}

Output:

Enter a string: sunday
sunday is not a palindrome

Find the Palindrome using for loop in java

Method 2
/*
 * Author: Zameer Ali
 * */
public class StringIsAPalindromeOrNot {

	public static void main(String[] args) {
		
		String s = "arora";
		String rev = "";
		for (int i = s.length()-1; i >=0 ; i--) 
			rev=rev+s.charAt(i);
		if(s.equals(rev))
			System.out.println("String is palindrome");
		else 
			System.out.println("String is not palindrome");

	}

}

Output:

String is palindrome

Java Program to Check Palindrome Number

Method 3
/*
 * Author: Zameer Ali
 * */
class Main {
  public static void main(String[] args) {
    
    int num = 3553, reversedNum = 0, remainder;
    
    // store the number to originalNum
    int originalNum = num;
    
    // get the reverse of originalNum
    // store it in variable
    while (num != 0) {
      remainder = num % 10;
      reversedNum = reversedNum * 10 + remainder;
      num /= 10;
    }
    
    // check if reversedNum and originalNum are equal
    if (originalNum == reversedNum) {
      System.out.println(originalNum + " is Palindrome.");
    }
    else {
      System.out.println(originalNum + " is not Palindrome.");
    }
  }
}

Output:

3553 is Palindrome.