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.example.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
* @return
*/
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);
}
}
Wayan Saryada
Founder at Kode Java Org
I am a programmer, a runner, a recreational diver, currently live in the island of Bali, Indonesia. Mostly programming in Java, Spring Framework, Hibernate / JPA. If these posts help, you can support me, buy me a cup of coffee or tea. Thank you 🥳
Latest posts by Wayan Saryada (see all)
- How do I set the time of java.util.Date instance to 00:00:00? - October 24, 2019
- How to Install Consolas Font in Mac OS X? - March 29, 2019
- How do I clear the current command line in terminal? - February 14, 2019