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:

• Convert the string to an array of chars;
//Example which we going to use to check if it is palindrome
//Transform string to array of chars to compare each character
char[] charArray = example.toCharArray();
• Set two pointers. One at the beginning of the array list and another at the end;
//Setting the start point
int leftPointer = 0;
//Setting the end point
int rightPointer = charArray.length - 1;
• Set the flag if the string is palindrome or not. By default, I would set it to true;
//Result of the checking
boolean result = true;
• Start looping the array list until the left point is less than the right pointer;
//Comparing first element and last and move to the middle
while (leftPointer < rightPointer)
• Check if the left pointer element is equal to the right pointer element. Stop the loop and return false if elements are not identical;
//Comparing first element with last and this way every element in array
if (charArray[leftPointer] != charArray[rightPointer]) {
result = false;

return;
}
• If elements of the current iteration are equal to each other. We should move both of them in the direction of the middle of the array list;
//Moving start point to next element right of current one
leftPointer++;
//Moving end point to next element left of current one
rightPointer--;

All steps schema:

Full code solution:

private void checkIfStringIsPalindrome() {
//Example which we going to use to check if it is palindrome

//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.