Welcome to our step-by-step tutorial on how to build a palindrome checker using Java.
Are you ready to dive into the world of programming and learn how to create a powerful tool that can detect palindromes? Look no further, as we have got you covered!
In this tutorial, we will walk you through the process of building a palindrome checker from scratch using Java. Whether you are a beginner or an experienced programmer, our detailed instructions and explanations will ensure that you understand every step of the process.
A palindrome is a word, phrase, or sequence of characters that reads the same backward as forwards.
By creating a palindrome checker, you will be able to input any text and determine whether it is a palindrome or not. This can be a useful tool for language processing, game development, or simply for fun! So, grab your coding tools and get ready to unleash your programming skills.
Let’s get started on building your very own palindrome checker using Java!
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.
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
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.
Now let’s implement the recursive method isPalindrome ( ) which will take as input a string, and will return as a 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 lowercase.
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