Palindrome is a popular title for programming training. You may compare a pair of chars in a string, or a pair of words in a String array. Many homeworks are made in this way.
Further more, in Java we may play with all the speaking languages in the world, such as Chinese, Korean, Japanese, Russian, and so on since Java uses Unicode for its char type. I have written a simple program to show the usefulness of both the Java char and the StringBuffer which provides a reverse() method to reverse any string. Please check my code and provide comments.
import java.io.*;
import javax.swing.JOptionPane;
public class Palindrome{
static boolean palindromeCouplet (String s) {
StringBuffer s1 = new StringBuffer(s);
return ((s.compareTo(new String(s1.reverse()))==0) ? true :false) ;
}
public static void main(String[] args) {
while(true){
String str=JOptionPane.showInputDialog( null,
"Type in a word!\nPress cancel to ternimate。",
"IS A PALINROME",
JOptionPane.QUESTION_MESSAGE);
if (str == null) {
System.out.println("Bey for now");
break;
}
JOptionPane.showMessageDialog( null,
"Palindrome: " + palindromeCouplet(str),
str + " IS A PALIDROME? ",
JOptionPane.INFORMATION_MESSAGE);
}
}
}