Pages

Java reverse String and simplest palindrome checker

Today, i have learn something new and it's totally simple. Sometime when don't know about that, we will use our way that take a time to complete a simple task. What i learn today is, how to reverse String. The easiest way to reverse a String for example from "i love you" to "uoy evol i" is using method reverse() in StringBuffer class. It is also very useful especially when you want to create a palindrome checker program. Your program will be the shortest palindrome checker program in the world i think, hehehehe. I have also created a new one before, but it quit long. You can review it by click here. Now we will go to coding part. I will create two simple programs. Firstly to reverse "i love you" to "uoy evol i". Secondly i will create my shortest palindrome checker program that i ever made. Don't waste our time by hear what i said.

*************************************************************
COMPLETE SOURCE CODE FOR : ReverseString.java
*************************************************************


public class ReverseString
{
public static void main(String[]args)
{
String a="i love you";
StringBuffer b=new StringBuffer(a);

System.out.println(b.reverse());
}
}


*************************************************************
LIKE USUAL, JUST COMPILE AND EXECUTE IT
*************************************************************

*************************************************************
COMPLETE SOURCE CODE FOR : MySimplestPalindromeChecker.java
*************************************************************


import javax.swing.JOptionPane;

public class MySimplestPalindromeChecker
{
public static void main(String[]args)
{
String a=JOptionPane.showInputDialog(null,"What word that you want to check ?","Palindrome Checker",JOptionPane.QUESTION_MESSAGE);

if(a!=null)
{
String b=a;
StringBuffer c=new StringBuffer(b);
if(a.equals(c.reverse().toString()))
{
JOptionPane.showMessageDialog(null,"THIS IS A PALINDROME WORD");
}
else
{
JOptionPane.showMessageDialog(null,"THIS IS NOT A PALINDROME WORD");
}
}
}
}


*************************************************************
JUST COMPILE AND EXECUTE IT
*************************************************************
Related Posts Plugin for WordPress, Blogger...