This code checks a string to determine if it is a palindrome or not. A palindrome is a word, phrase, or sequence that reads the same backward as forward.
package org.kodejava.lang;
public class PalindromeChecker {
public static void main(String[] args) {
String text = "Sator Arepo Tenet Opera Rotas";
PalindromeChecker checker = new PalindromeChecker();
System.out.println("Is palindrome = " + checker.isPalindrome(text));
}
/**
* This method checks the string for palindrome. We use StringBuilder to
* reverse the original string.
*
* @param text a text to be checked for palindrome.
* @return <code>true</code> if a text is palindrome.
*/
private boolean isPalindrome(String text) {
System.out.println("Original text = " + text);
String reverse = new StringBuilder(text).reverse().toString();
System.out.println("Reverse text = " + reverse);
// Compare the original text with the reverse one and ignoring its case
return text.equalsIgnoreCase(reverse);
}
}
Latest posts by Wayan (see all)
- How do I get number of each day for a certain month in Java? - September 8, 2024
- How do I get operating system process information using ProcessHandle? - July 22, 2024
- How do I sum a BigDecimal property of a list of objects using Java Stream API? - July 22, 2024