If the given input is a palindrome, Print it as it is, if not reverse the string and print.
Input : abcd
Output: dcba
The question is hidden with the meaning of not doing complex things and understanding them clearly.
Answer: whatever the input, just need to reverse the string.
Code:
package wordpress;
public class Palindrom {
public static String reverseIfItsPalidrome(String input) {
StringBuilder stringBuilder = new StringBuilder(input);
return stringBuilder.reverse().toString();
}
public static void main(String[] args) {
//Palidrome String
System.out.println(reverseIfItsPalidrome("MADAM"));// MADAM
//non Palidrome String
System.out.println(reverseIfItsPalidrome("ABCD"));//DCBA
}
}
