Hi All,
Greetings of the day!
Today we will learn how to check given String is Palindrome or not in Java so let's start.
What is Palindrome String
Palindrome String is a String that is the same when we read it from any end i.e. from start or end
Example ABBA
How to check given String is Palindrome Or Not In Java
A simple way to check Palindrome String is to reverse a given string and then check if the original String and reversed string are equal or not. If both strings are equal we can say the given String is a Palindrome String.
So here is the sample code for the same.
package com.demo.programs; public class CheckPalindrome { public static void main(String[] args) { String originalString="ABBA"; String reverseString=""; char[] chars=originalString.toCharArray(); for(int i=chars.length-1;i>=0;i--){ reverseString=reverseString+chars[i]; } if(originalString.equalsIgnoreCase(reverseString)){ System.out.println("Given String is palindrome"); }else{ System.out.println("Given String is not palindrome."); } } }
To reverse a String we are using for loop over character array initialized from original String.
Find First Repeating Character In String
Count No Of Occurrences Of Element In List + Find Duplicate Elements In List + Java 8
Sorting HashSet In Ascending & Descending order + Java 8
Comparator and Comparable In Java + Difference Between Comparator and Comparable + Java 8
Java 8 Stream - Intermediate Operations With Examples
Java 8 Stream - Terminal Operations With Examples
0 Comments
If you have any doubts let me know.