5 different ways to create Palindrome Program in Java

In this tutorial, you’ll learn how to determine whether a string or number is palindrome using java.

In Java, a palindrome number is a number that is the same after being reversed. Palindrome numerals include 121, 707, and 60606 for example. In the case of string palindromes, it could be anything like SAAS, NAD, and so on.

Easily A palindrome is a phrase that is the same when reading both forward and backward.

There are several ways to check the palindrome; let us go through them one by one.

  • Find Number Palindrome without any default library methods
  • Find Palindrome using substring()
  • Find Palindrome using charAt()
  • Find Palindrome using StringBuilder()
  • Find Palindrome using Streams

1. Find Number Palindrome without any default library methods

public static void main(String[] args) {
	System.out.println("Find Number Palindrome without library methods : 2332-> "+Palindrome.findNumberPalindrome(2332));
}
private static String findNumberPalindrome(int number) {
		int temporaryValue = 0, reversedNumber = 0;
		int originalNumber = number;
		while (number != 0) {
			 temporaryValue = number % 10;
			 reversedNumber = reversedNumber * 10 + temporaryValue;
		      number /= 10;
		}
		    if (originalNumber == reversedNumber) {
		      return "is a Palindrome";
		    } else {
		    	return "is not a Palindrome";
		    }
}

Output :

Find Number Palindrome without any methods : 2332-> is a Palindrome


2. Find Palindrome using substring()

public static void main(String[] args) {
	System.out.println("Find Palindrome using substring() : Earth -> "+Palindrome.findStringPalindrome("Earth"));
}
private static String findStringPalindrome(String inputData) {
		
		String reversedString = "";
		for(int i=inputData.length()-1;i>=0;i--) {
			reversedString = reversedString + inputData.substring(i,i+1);
		}
		    if (inputData.equals(reversedString)) {
		      return "is a Palindrome";
		    } else {
		    	return "is not a Palindrome";
		    }
	}

Output :

Find Palindrome using substring() : Earth -> is not a Palindrome


3. Find Palindrome using charAt()

public static void main(String[] args) {
	System.out.println("Find Palindrome using charAt() -> : SaaS -> "+Palindrome.findPalindromeUsingCharAt("SaaS"));
	
}

private static String findPalindromeUsingCharAt(String inputData) {
		
		String reversedString = "";
		for(int i=inputData.length()-1;i>=0;i--) {
			reversedString = reversedString + inputData.charAt(i);
		}
		    if (inputData.equals(reversedString)) {
		      return "is a Palindrome";
		    } else {
		    	return "is not a Palindrome";
		    }
	}

Output :

Find Palindrome using charAt() -> : SaaS -> is a Palindrome


4. Find Palindrome using StringBuilder()

public static void main(String[] args) {
	System.out.println("Find Palindrome using StringBuilder() : NaN -> "+Palindrome.findPalindromeUsingStringBuilder("NaN"));
	
}

private static String findPalindromeUsingStringBuilder(String inputData) {
		StringBuilder sb = new StringBuilder(inputData);
		    if (inputData.equals(sb.reverse().toString())) {
		      return "is a Palindrome";
		    } else {
		    	return "is not a Palindrome";
		    }
	}

Output :

Find Palindrome using StringBuilder() : NaN -> is a Palindrome


5. Find Palindrome using Streams

public static void main(String[] args) {
	System.out.println("Find Palindrome using Streams : RAAR -> "+Palindrome.findPalindromeUsingStreams("NaN"));
}
private static String findPalindromeUsingStreams(String inputData) {
		return IntStream.range(0, inputData.length() / 2)
				.noneMatch(i -> inputData.charAt(i) != inputData.charAt(inputData.length() - i - 1)) ? "is a Palindrome" : "is not a Palindrome";
	}

Output :

Find Palindrome using Streams : RAAR -> is a Palindrome

Related Posts

Leave a Reply

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