Palindrome Detector on Java

What will This Program Do?

This program will figure out if a given word is a palindrome or not. A palindrome is a word that is the same spelled backwards. For example, civic spelled backwards is civic.

How will the Program Work?

The code below is a simple way to make the program work.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.patterns;
public class PalendromeDetector2 {
    public static void main(String[] args) {
        String mystring = "palendrome";
        boolean isPal = true;
        for (int i = 0; i < mystring.length(); i++) {
            int j = mystring.length() - 1 - i;
            if (mystring.charAt(i) != mystring.charAt(j)) {
                isPal = false;
                break;
            }
        }
        if (isPal == true) {
            System.out.println(mystring + " is a palendrome");
        } else {
            System.out.println(mystring + " is not a palendrome");
        }
    }
}

How this Program Works

The first thing we are doing is defining a variable. Mystring is going to be the word that we are referring to. If mystring is pumpkin, Java will check if pumpkin is a palindrome.

Next we are defining another variable, a boolean this time. isPal represents if the word is a palendrome or not. Right now, we define isPal as true, although it might not be a palindrome. We will use this variable later.

After that is a for loop. There are two variables in this loop: i and j.

The first line is for(int i = 0; i < mystring.length( ); i++). The loop starts out with i being 0. I has to be less than mystring.length( ) to continue the loop. Mystring.length is the length of the value of mystring. Since pumpkin has 7 letters, the length is 7. I + 1 after every loop. To understand this better, go to my post on for loops.

Before we continue, there is one thing you need to understand. In a string, every letter is assigned a value. The first letter is 0, the second is 1, and so on. The last letter in pumpkin is 6, because the first letter is 0, not 1.

The next line is int j = mystring.length( ) -1 – i. The variable i is changing every time; it is increasing by one. The first time the loop goes around, j = 7 – 1 – 0, which is 6. The next time it will be one less, because i is increasing by one.

Inside the loop, there is an if statement after the first line. When you say mystring.charat, you are getting a specific letter of the string. If in the parenthesis you put the number one, it will identify the letter with the value of one; this is the second letter. You can also put an int variable in the parenthesis.

If mystring.charat(i) != mystring.charat(j), isPal = false, break. This is basically what is in the if loop. i begins at 0, so it is identifying the first letter. j has been programmed to be the last letter. If the first letter does not equal the last letter, change the value of isPal to false, and stop the loop. Since i increases by one and j decreases by one every time, it will work perfectly.

In simpler words, if letter 0 != letter 6(the last letter) do the action in the loop. if letter 1 != letter 5, do the action in the loop. It keeps going on.

For example, if the word is pumpkin, it will go like this: since p is not equal to n, isPal = false, stop the loop.

If the word is a palindrome, the loop will continue as many times as the length of the string. For example, if the word is civic, it will go like this:

1st loop around: c !=c? false, continue the loop. isPal is still true

2nd loop around: i != i? false, continue the loop. isPal is still true

3rd loop around: v != v?  false, continue the loop. isPal is still true

4th loop around: i != i?  false, continue the loop. isPal is still true

5th loop around c != c?  false, continue the loop. isPal is still true

At this point, the  for loop ends because civic is only 5 letters.

 

Now the boolean isPal truly represents if the string is a palindrome. If the word was a palindrome, the for loop was completed and isPal was still true. If the word was not a palindrome, isPal became false, and the loop ended immediately.

Now we use an if statement outside the loop. If isPal is true, print out mystring “is a palindrome”.else, print out mystring “is not a palindrome”.

 

Leave a comment