Skip to content
Related Articles

Related Articles

Improve Article

Java program to check whether a string is a Palindrome

  • Difficulty Level : Basic
  • Last Updated : 01 Oct, 2021
Geek Week

A string is said to be a palindrome if it is the same if we start reading it from left to right or right to left. So let us consider a string “str”, now the task is just to find out with its reverse string is the same as it is. 

Illustration:

Input : str = "abba" 
Output: Yes
Input : str = "geeks"
Output: No  

Approach: Our approach will be that initially, we will take two pointers i pointing to the start of the string and j pointing to the end of the string. Keep incrementing i and decrementing j while i < j and at every step check whether the characters at these pointers are same or not. If not then the string is not a palindrome else it is.

Example 1:

Java




// Java program to check whether a
// string is a Palindrome
// Using two pointing variables
  
// Main class
public class GFG {
  
    // Method
    // Returning true if string is palindrome
    static boolean isPalindrome(String str)
    {
  
        // Pointers pointing to the beginning
        // and the end of the string
        int i = 0, j = str.length() - 1;
  
        // While there are characters to compare
        while (i < j) {
  
            // If there is a mismatch
            if (str.charAt(i) != str.charAt(j))
                return false;
  
            // Increment first pointer and
            // decrement the other
            i++;
            j--;
        }
  
        // Given string is a palindrome
        return true;
    }
  
    // Method 2
    // main driver method
    public static void main(String[] args)
    {
        // Input string
        String str = "geeks";
  
        // passing bool function till holding true
        if (isPalindrome(str))
  
            // It is a pallindrome
            System.out.print("Yes");
        else
  
            // Not a pallindrome
            System.out.print("No");
    }
}
Output: 



No

 

Example 2:

Java




// Java Program to check Whether the String is Palindrome
// or Not
  
// Main class
class GFG {
  
    // Method 1
    // Returns true if string is a palindrome
    static boolean isPalindrome(String str)
    {
  
        // Pointers pointing to the beginning
        // and the end of the string
        int i = 0, j = str.length() - 1;
  
        // While there are characters to compare
        while (i < j) {
  
            // If there is a mismatch
            if (str.charAt(i) != str.charAt(j))
                return false;
  
            // Increment first pointer and
            // decrement the other
            i++;
            j--;
        }
  
        // Given string is a palindrome
        return true;
    }
  
    // Main driver method
    public static void main(String[] args)
    {
        String str = "geeks";
        String str2 = "racecar";
  
        // For string 1
        System.out.print("String 1 :");
  
        if (isPalindrome(str))
            System.out.print("It is a pallindrome");
        else
            System.out.print("It is not a pallindrome");
  
        // new line for better readibility
        System.out.println();
  
        // For string 2
        System.out.print("String 1 :");
        if (isPalindrome(str2))
            System.out.print("It is a pallindrome");
        else
            System.out.print("It is not a pallindrome");
    }
}
Output
String 1 :It is not a pallindrome
String 1 :It is a pallindrome

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :