Easily Check the string if is a palindrome or not (Java). – IT Blog
IT Blog
Boris IT Expert

Easily Check the string if is a palindrome or not (Java).

Boris~November 6, 2020 /Array List

In case you want to check if the string is a palindrome, you should follow several steps. First of all, you have to transform the string into an array of chars. Then you need to set two pointers. The fundamental idea is to compare pointers’ chars. With the help of the “while loop,” we compare the first and the last element. In case they are equal, we move our pointers one element to the center. As a result, if some chars are not equal during the loop, the string is not a palindrome. Otherwise, pointers face each other in the middle and finish the process.

Check if the string is a palindrome (Java). Step-by-step implementation.

If you want to check if the string is a palindrome, you have to follow the detailed instructions below:

//Example which we going to use to check if it is palindrome
        String example = "madam";
//Transform string to array of chars to compare each character
        char[] charArray = example.toCharArray();
//Setting the start point
        int leftPointer = 0;
//Setting the end point
        int rightPointer = charArray.length - 1;
//Result of the checking
        boolean result = true;
//Comparing first element and last and move to the middle
        while (leftPointer < rightPointer) 
 //Comparing first element with last and this way every element in array
          if (charArray[leftPointer] != charArray[rightPointer]) {
                result = false;
       
                return;
            }
string is palindrome or not
//Moving start point to next element right of current one
            leftPointer++;
//Moving end point to next element left of current one
            rightPointer--;
string is palindrome or not

All steps schema:

string is palindrome or not

Full code solution:

private void checkIfStringIsPalindrome() {
//Example which we going to use to check if it is palindrome 
    String example = "madam";

//Transform string to array of chars to compare each character
    char[] charArray = example.toCharArray();

//Setting the start point
    int leftPointer = 0;

//Setting the end point
    int rightPointer = charArray.length - 1;

//Result of the checking
    boolean result = true;

//Comparing first element and last and move to the middle
    while (leftPointer < rightPointer) {

//Comparing first element with last and this way every element in array
        if (charArray[leftPointer] != charArray[rightPointer]) {
            result = false;
            Log.d("arrayList", "it is Palindrome - " + result);
            return;
        }

//Moving start point to next element right of current one
        leftPointer++;

 //Moving end point to next element left of current one
        rightPointer--;
    }

    Log.d("arrayList", "it is Palindrome - " + result);
}

In this article, you figured out is the string is a palindrome (Java).

Full code and more examples in my facebook group.

Useful links:

Leave Any Questions Related To This Article Below!