Is This String A Palindrome
A palindrome is a word that is exactly the same whether read forwards or backwards. What you need to do is write a function that returns true if this string provided is a palindrome.
The idea is simple: solve the coding problem as efficiently as you can, in any language or framework that you find suitable.
Note: Even though there really is nothing stopping you from finding a solution to this on the internet, try to keep honest, and come up with your own answer. It’s all about the participation!
public class PalendromePuzzle {
public static void main(String… args) {
String string = “abcdcba “;
System.out.println(“is it palendrome ” + isPalendrome1(string.trim()));
System.out.println(“is it palendrome ” + isPalendrome2(string.trim()));
}
// core implementation
private static boolean isPalendrome1(String string) {
string = string.trim();
int stringLength = string.length();
for (int index = 0; index < stringLength / 2; index++) {
if (!(string.charAt(index) == string.charAt(stringLength – index – 1))) {
return false;
}
}
return true;
}
// liveraged string buffer API
private static boolean isPalendrome2(String string) {
return new StringBuffer(string).reverse().toString().equalsIgnoreCase(string);
}
}
LikeLike