Java - String Manipulation, Palindrome Reverse Order

Guest Posts Paolo Nikko Nuñal

Java has many built in functions when it comes to manipulating strings. One of the most common problems that you will encounter if you are learning Java in school is that your instructor will have a problem with regards to reversing a word. If the word can still be read even it is being reversed is called a Palindrome. In this tutorial you are going to learn on how you can manipulate a string and tell whether or not the string is a Palindrome or not.

public class Palindrome
{
 public static void main(String args[])
 {
 String original = "yasay";
 String reverse = "";
 
 for(int i = original.length() - 1; i >= 0; i--)
 {
 reverse = reverse + original.charAt(i);
 }
 
 if(original.equals(reverse))
 {
 System.out.println("String is Palindrome");
 }
 else
 {
 System.out.println("String is not Palindrome");
 }
 }
}


The methods we used are the length() and charAt(). First of all, the length() method gets the length of the String and returns an integer. In order to reverse the string we try to get the total length of the string and start getting the characters at the last part or index. Since we can get it with the length(), we then gave the value for our counter with the length and at the same time as it loops through, it will continue to get each character on the String and concatenate it through our reverse variable.

The next thing we did is checking the original string with the reversed string using the method .equals(). It will return either true or false regarding on the result.