Java - Palindrome String
Another common Java exercise is to verify if a String is a Palindrome
The algorithm is:
- Split the String into an array with all the characters
- Reverse the array
- Join the array again
- Check if the reversed string is equal to the input
Reversing the array
public class StringReverser {
public static String reverse(String s1){
if(isNull(s1)){
return null;
}
List<String> charList = asList(s1.split(""));
Collections.reverse(charList);
return String.join("", charList);
}
}
Palindrome class
public class Palindrome {
public static boolean isPalindrome(String s1){
if(isNull(s1)){
return false;
}
String reversed = StringReverser.reverse(s1);
return reversed.equals(s1);
}
}
Source code: Palindrome