Given a string and we have to check whether it is palindrome string or not.
A string that is equal to its reverse string is known as palindrome string. To implement the program for checking whether a given string is a palindrome or not, we have created a function "isPalindrome()".
In the function,
- We are checking for whether a string is an empty string or not – if the string is an empty string then throwing an error.
- Then, we are converting string to uppercase to make comparison case insensitive.
- Then, running a loop from 0 to len/2, to compare the first character with last character, the second character with second last character and so on..., and checks whether they are equal or not if both the elements are equal it goes for the next one. If not, then code returns false. Going on comparing first and last elements of the string if it reaches the length/2 mark then the loop ends, and return true for Palindrome.
-
Java code for checking string palindrome
Output