String Palindrome Program in Java

A phrase, word, number, or collection of characters that reads the same both forward and backward is known as a palindrome. This blog describes how to use a program to check for a palindrome in Java. We will understand the concept of the palindrome before learning about palindromes in Java.

What is a String Palindrome?

If a string remains unchanged whether it is read from the left to the right or the right to the left, it is said to be a palindrome. So let’s take a look at the string "str" The task now is to determine whether its reverse string is the same as it is.

Example:

Input : str = "abba" 
Output: Yes

Input : str = "abcd"
Output: No 

Algorithm to Find the String Palindrome in Java:

  • START
  • Take input from the user or initialize it manually (string).
  • Check if the length is equal to zero or one
    • Print it is a palindrome.
  • Check each character in substring from the front and rear; if found, equal
    • Print it is a palindrome
  • If steps 3 and 4 fail
    • Print it is not Palindrome
  • END

Naive Approach: We can determine whether a given string is a palindrome by comparing the original string to its reversed form.
Below is the implementation:

String Palindrome Java Program

import java.io.*;

class PrepBuddy {
    public static boolean isPalindrome(String str)
    {
        String rev = "";

        boolean ans = false;

        for (int i = str.length() - 1; i >= 0; i--) {
            rev = rev + str.charAt(i);
        }

        if (str.equals(rev)) {
            ans = true;
        }
        return ans;
    }
    public static void main(String[] args)
    {
        String str = "Abcd";

        str = str.toLowerCase();
        boolean A = isPalindrome(str);
        System.out.println(A);
    }
}

Output:

False

In the previous example, if we substitute ABba for abba, the output should be yes. As a result, before we check for a palindrome, we must alter the case of the string to either lowercase or uppercase. We will obtain surprising outcomes if we do not do this. This is due to the fact that the compiler validates the characters based on their ASCII value, and A’s ASCII value is not the same as a’s.

String Palindrome Optimized Approach

Our strategy will be to first transform the string to lowercase. Then we’ll take two pointers, i pointing to the beginning of the string and j pointing to the end. Continue to increase i and decrease j while I < j and verify whether the characters at these addresses are the same or not at each step. If not, the string is not a palindrome otherwise, it is.

class PrepBytes {

    static boolean isPalindrome(String str)
    {

        int i = 0, j = str.length() - 1;

        while (i < j) {

            if (str.charAt(i) != str.charAt(j))
                return false;

            i++;
            j--;
        }

        return true;
    }

    public static void main(String[] args)
    {
        String str = "abba";

        str = str.toLowerCase();
        if (isPalindrome(str))

            System.out.print("Yes");
        else

            System.out.print("No");
    }
}

Output:

Yes

String Palindrome Recursive Approach

The strategy is straightforward. We will examine the first and last values of the string using recursion, as we did with the two-pointer technique.

  • We'll use two pointers, i pointing to the beginning of the string and j pointing to the end.
  • Continue to increase i and decrease j while I =j condition is satisfied, print true, otherwise, print false.
    Below is the implementation:
import java.io.*;
class PrepBytes {
    public static boolean isPalindrome(int i, int j,
                                    String A)
    {
        if (i >= j) {
            return true;
        }

        if (A.charAt(i) != A.charAt(j)) {
            return false;
        }

        return isPalindrome(i + 1, j - 1, A);
    }

    public static boolean isPalindrome(String A)
    {
        return isPalindrome(0, A.length() - 1, A);
    }

    public static void main(String[] args)
    {
        String A = "PrepBytes";

        A = A.toLowerCase();
        boolean str = isPalindrome(A);
        System.out.println(str);
    }
}

Output:

No

Conclusion:
This takes us to the end of the article on Java Palindromes. I hope this helps you improve your Java abilities. Check out this comprehensive Java lesson to become a master in Java programming.

Other Java Programs

Java Program to Add Two Numbers
Java Program to Check Prime Number
Java Program to Check Whether a Number is a Palindrome or Not
Java Program to Find the Factorial of a Number
Java Program to Reverse a Number
Java Program to search an element in a Linked List
Program to convert ArrayList to LinkedList in Java
Java Program to Reverse a linked list
Java Program to search an element in a Linked List
Anagram Program in Java
Inheritance Program in Java
Even Odd Program in Java
Hello World Program in Java
If else Program in Java
Binary Search Program in Java
Linear Search Program in Java
Menu Driven Program in Java
Package Program in Java
Leap Year Program in Java
Array Programs in Java
Linked List Program in Java
String Programs in Java
Star Program in Java
Number Pattern Program in Java
For Loop Program In Java
Pattern Program in Java

Leave a Reply

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