Palindrome program in Java

According to Wikipedia, a palindrome is a word, number, phrase, or other sequences of characters, which we can read the same backward as forward. Such examples are “Madam”, “Solos”, “Amore, Roma” or dates such as 02/02/2020. Today, we will build a palindrome program in Java, which checks if a string is a palindrome.

What is a palindrome program?

A palindrome program checks if a string is a palindrome, which means if we can read it the same backward as forward.

First, let’s write the algorithm of the program.

Step 1: Give a string as input .
Step 2: Initialize two index variables, so one can move backward and the other forward.
Step 3: Calculate if the letters in the indexes’ position are the same.
Step 4: If yes, increase one index and decrease the other, and repeat step 3.
Step 5: If the letters are not the same, we can conclude the string is not a palindrome.

Palindrome PROGRAM IN JAVA

First, let’s write a method that checks if the string is a palindrome. This method will take the string as input.

static void checkString(String str)

 

The string input should be edited so it does not contain white spaces, and all the letters are lower case, so we can compare them.

str = s1.toLowerCase(); 
str = str.replaceAll("\\s+","");

 

The two counters will be i and j, which will hold the index of each letter we will compare.

int i = 0; 
int j = str.length() - 1;

 

The palindrome has the same letters for i and j positions, so we will check if this expression is true:

str.charAt(i) != str.charAt(j)

 

The i variable will go forward and the j variable will go backward. This continues until i reach j.

So, in a while loop, we will increase i and decrease j:

i++; 
j--;

 

The final version of the Palindrome program in Java looks like this:

public class Palindrome_Demo {
 static void checkString(String s1)
 {
  String str;
  str = s1.toLowerCase();
  str = str.replaceAll("\\s+","");

  int i = 0;
  int j = str.length() - 1;
  boolean result=true;

  while (i < j) {
   if (str.charAt(i) != str.charAt(j))
    result=false;
   i++;
   j--;
  }
  if (result)
   System.out.print(s1+" is Palindrome");
  else
   System.out.print(s1+" is Not Palindrome");
  }
  public static void main(String[] args)
  {
   String s = "My gym";
   checkString(s);
  }
}

 

In Console, the result is:

My gym is Palindrome
Think about A Recursive Version

Palindrome checker has also a recursive version. It is the typical case where the solution can be seen as a recursion: the way we check the whole string, we can check its substring.

Let’s write the algorithm of the recursive method.

Step 1: Give a string as input.
Step 2: If the length is 0 or 1 then the string is a palindrome.
Step 3: Check if the first and the last character of the string are the same.
Step 4: If they are the same then do the same thing for a substring, with the first and last character removed.
Step 5: Repeat step 3 until the
string has the length 0 or 1 or the condition fails.
Step 6: If the letters are not the same, we can conclude the string is not a palindrome.

Palindrome PROGRAM, recursive, IN JAVA

Now lets implement the recursive method isPalindrome ( ) which will take as input a string, and will return as result a boolean value.

First, the string is checked if it has the length zero or one.

s.length() == 0 || s.length() == 1

 

The condition to continue the same check to the substring is:

if(s.charAt(0) == s.charAt(s.length()-1))

 

This means we will continue to apply the same check only if the first and the last character of the string are the same. If so, let’s call the same method isPalindrome ( ), but with the new string as input: the string minus first and last char, so let’s call it:

isPalindrome(s.substring(1, s.length()-1))

 

The recursion stops if the substring has the length zero or one.

Note that in the main method, we will edit the string, remove the white spaces, and set all the letters in lower case.

The full program of palindrome in Java, the recursive version is:

public class Palindrome_Rec_Version {
 public static boolean isPalindrome(String s){   
  if(s.length() == 0 || s.length() == 1)
   return true; 
  if(s.charAt(0) == s.charAt(s.length()-1))
   return isPalindrome(s.substring(1, s.length()-1));
  return false;
}

 public static void main(String[]args) {
  String str="no melon no lemon";
  str=str.toLowerCase();
  str=str.replaceAll("\\s+","");
  if(isPalindrome(str))
   System.out.println(str + " is a palindrome");
  else
   System.out.println(str + " is not a palindrome");
 }
}

 

In Console:

nomelonnolemon is a palindrome

 

Hoping this was helpful. Keep coding…

>> For more Java Applications Tutorials, visit Code in Java