Programs

Java Program for String Palindrome

When a number remains the same even after reversal, it is referred to as a palindrome. Some examples of palindromes include 656, 232, 46764, and the like. Palindromes can also exist as strings such as MADAM (Palindrome Program in Java , n.d.). In Java, a basic algorithm may be applied to check whether a number is a palindrome. The basic steps included in the algorithm are summarized below:

  • Assign the number to a temporary variable
  • Obtain the reverse of the number
  • Compare the value of the number in the temporary variable with the reversed value
  • When there is no difference between the two values, print a message stating that it is a palindrome. Otherwise, print a message stating that the number is not a palindrome.

Check out our free technology courses to get an edge over the competition.

Several other approaches may be applied to check whether a given number or string is a palindrome. In the example below, two pointers are used to navigate from the beginning to the end of the input provided. The program confirms whether the supplied input is a palindrome (Java program to check whether a string is a Palindrome, 2019).

Learn Software Development Courses online from the World’s top Universities. Earn Executive PG Programs, Advanced Certificate Programs or Masters Programs to fast-track your career.

Explore our Popular Software Engineering Courses

Amid increased conversations around crypto and Blockchain technology, if you wish to educate yourself professionally on the topic, then upGrad’s Executive Post Graduate Programme in Software Development – Specialisation in Blockchain under IIIT- Bangalore is the right choice for you!

How to Check String Palindrome in Java

The code to check string palindrome in Java is as follows:

class Main {
  public static void main(String[] args) {
    String str = "Radar", reverseStr = "";
    int strLength = str.length();
    for (int i = (strLength - 1); i >=0; --i) {
      reverseStr = reverseStr + str.charAt(i);
    }
    if (str.toLowerCase().equals(reverseStr.toLowerCase())) {
      System.out.println(str + " is a Palindrome String.");
    }
    else {
      System.out.println(str + " is not a Palindrome String.");
    }
  }
}

Output:

Radar is a Palindrome String.

In this example, the “Radar” string has been stored in str. The following has been used for the code:

  • Reversing the string using the for loop
  • The loop operates from the beginning to the end of the string.
  • The charAt() method is useful for accessing every string character.
  • Every character is stored in reverseStr after being accessed in reverse order. 
  • If statement to compare str and reverseStr

The toLowerCase() method can transform both reverseStr and str to lowercase. It happens because Java is extremely case-sensitive. Therefore, R and r are two different values in Java.

The equals() method can determine whether there’s equality between two strings. 

In-Demand Software Development Skills

How to Check String Palindrome Program in Java

The code to check the string palindrome program in Java is as follows:

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
    // estore 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.

In this example, the number 3533 is stored in num and originalNum variables. The following has been used here:

  • While loop to reverse num and preserve the reversed number in reversedNum
  • To check if reversedNum is the same as the originalNum, the if…else has been used

How to Write the Palindrome Number Program in Java

To write the palindrome number program in Java, the following algorithm will be followed:

  • Use the number to look for palindromes.
  • Keep the number in a temporary variable.
  • Reverse the number.
  • Use the reversed number to compare it with the temporary number.
  • When both numbers are the same, you can print the palindrome number.
  • If both numbers are not the same, you will have to print not a palindrome number. 

Frequently Asked Questions

What is the palindrome Java program?

A palindrome string in Java remains the same when it is reversed. Some examples of palindromes include level and radar. Similarly, any number equal to the reverse of it is referred to as a palindrome number. A few examples of palindrome numbers include 1232 and 3533.

Can you call 121 a palindrome number in Java?

121 can be called a palindrome because it remains the same when it is reversed. But 123 is not a palindrome because it gets changed when it is reversed.

Which is the highest palindrome in Java?

The highest palindrome in any Java array is 1238321.

 

Want to share this article?

Plan your Software Development Career Now!

Leave a comment

Your email address will not be published. Required fields are marked *

Our Popular Software Engineering Courses

Get Free Consultation

Leave a comment

Your email address will not be published. Required fields are marked *

×
Get Free career counselling from upGrad experts!
Book a session with an industry professional today!
No Thanks
Let's do it
Get Free career counselling from upGrad experts!
Book a Session with an industry professional today!
Let's do it
No Thanks