Check the given String is Palindrome or not?
If a string is palindrome, it gives the same string even after reversing it.
For example, malayalam, madam, liril etc.., are palindromes.
The following steps can be followed to check the string:
- Let the string be 'str'.
- Preserve a copy of the original string 'str' in another string 'temp'.
- Convert the string 'str' into a StringBuffer object 'sb'.
- Reverse the string in sb.
- Store the reversed string in 'str' again.
- Now, compare the original string 'temp' with the reversed string 'str' . If they equal the given string is palindrome, else not a palindrome.
We are converting a String into StringBuffer to use the reverse() method of StringBuffer class.
It is not possible to use methods of a class on the objects of another class.
Java code to check if the given string is palindrome or not:
Output:
enter the String: Malayalam Malayalam is palindrome
enter the String: madam madam is palindrome
Palindrome Other Articles