Palindrome String

A string is said to be palindrome if it is the same from both the ends. For example : malayalam,rotor,civic,etc

Program 1 : Check Whether String is Palindrome (Using Reverse String)

public class StringPalindromeProg1 {
	
//  Java Program To Check Whether String is Palindrome (Using Reverse String)	

	public static boolean checkPalindrome(String str) {
	      
             str = str.toLowerCase();
             String revStr = "";
		
             for (int i = str.length() - 1; i >= 0; i--) {
                   revStr = revStr + str.charAt(i);
             }

                   return str.equals(revStr);
         }

         public static void main(String[] args) {
      
                String str1 = "malayalam";
	        String str2 = "motor";

                boolean checkStr1 = checkPalindrome(str1);
		boolean checkStr2 = checkPalindrome(str2);

		System.out.println("Given String is " + str1);
         
                if (checkStr1) {
                        System.out.println("Given String " + str1 + " is a palindrome" );
                } else {
                        System.out.println("Given String " + str1 + " is not a palindrome" );
                }
		
		        System.out.println("\nGiven String is " + str2);
                if (checkStr2) {
                        System.out.println("Given String " + str2 + " is a palindrome" );
                } else {
                        System.out.println("Given String " + str2 + " is not a palindrome" );
                }
    }
}
Output :

Given String is malayalam
Given String malayalam is a palindrome

Given String is motor
Given String motor is not a palindrome

Program 2 : Check Whether String is Palindrome (Using Two Pointer)

public class StringPalindromeProg2 {
	
//  Java Program To Check Whether String is Palindrome (Using Two Pointer)	

	public static boolean checkPalindrome(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 str1 = "malayalam";
        String str2 = "motor";

        boolean checkStr1 = checkPalindrome(str1);
        boolean checkStr2 = checkPalindrome(str2);

        System.out.println("Given String is " + str1);
        if (checkStr1) {
            System.out.println("Given String " + str1 + " is a palindrome" );
        } else {
            System.out.println("Given String " + str1 + " is not a palindrome" );
        }
		
	System.out.println("\nGiven String is " + str2);
        if (checkStr2) {
            System.out.println("Given String " + str2 + " is a palindrome" );
        } else {
            System.out.println("Given String " + str2 + " is not a palindrome" );
        }
    }

}
Output :

Given String is malayalam
Given String malayalam is a palindrome

Given String is motor
Given String motor is not a palindrome

Program 3 : Check Whether String is Palindrome (Using Recursion)

public class StringPalindromeProg3 {

//  Java Program To Check Whether String is Palindrome (Using Recursion)	

	public static boolean checkPalindrome(String str, int start, int end) {
	      
        if (start == end)    
            return true;
		
        if ((str.charAt(start)) != (str.charAt(end))) 
            return false;   
		
        if (start < end + 1) 
            return checkPalindrome(str, start + 1, end - 1); 
		
        return true; 
    }

    public static boolean checkPalindrome(String str) {
         int n = str.length(); 
		 
         if (n == 0) 
            return true;   
		
         return checkPalindrome(str, 0, n - 1); 
    }

    public static void main(String[] args) {
      
        String str1 = "malayalam";
	String str2 = "motor";

        boolean checkStr1 = checkPalindrome(str1);
        boolean checkStr2 = checkPalindrome(str2);

	System.out.println("Given String is " + str1);
        if (checkStr1) {
            System.out.println("Given String " + str1 + " is a palindrome" );
        } else {
            System.out.println("Given String " + str1 + " is not a palindrome" );
        }
		
	System.out.println("\nGiven String is " + str2);
        if (checkStr2) {
            System.out.println("Given String " + str2 + " is a palindrome" );
        } else {
            System.out.println("Given String " + str2 + " is not a palindrome" );
        }
    }
}
Output :

Given String is malayalam
Given String malayalam is a palindrome

Given String is motor
Given String motor is not a palindrome

Program 4 : Check Whether String is Palindrome (Using StringBuilder)

public class StringPalindromeProg4 {

//  Java Program To Check Whether String is Palindrome (Using StringBuilder)	
	
	 public static void main(String[] args) {
	      
	        String str1 = "malayalam";
		String str2 = "motor";
	        StringBuilder strBuild1 = new StringBuilder(str1);
	        StringBuilder strBuild2 = new StringBuilder(str2);


	        strBuild1.reverse();
		strBuild2.reverse();

		System.out.println("Given String is " + str1);

	        if (str1.equals(strBuild1.toString())) {
	           System.out.println("Given String " + str1 + " is a palindrome" );
	        } else {
	           System.out.println("Given String " + str1 + " is not a palindrome" );
	        }
			
		System.out.println("\nGiven String is " + str2);

		if (str2.equals(strBuild2.toString())) {
	           System.out.println("Given String " + str2 + " is a palindrome" );
	        } else {
	           System.out.println("Given String " + str2 + " is not a palindrome" );
	        }
	}	
}
Output :

Given String is malayalam
Given String malayalam is a palindrome

Given String is motor
Given String motor is not a palindrome
Scroll to Top