Java how do you check if a string is a palindrome

Hey guys!! Welcome to flower brackets blog. In this article we are going to learn java how do you check if a string is a palindrome.

Java how do you check if a string is a palindrome

Palindrome java : definition of palindrome

Palindrome string is a string where string remains same when reversed or a word that is spelled the same way forwards and backwards.

For example the word level, dad, mom, madam, abcba, etc.,

Java how do you check if a string is a palindrome?

We are going to start off by storing our palindrome in a string variable like,

String str = "level";

Next we are going to define the start position of the string which will be used in our substring method later on.

We are going to do our start position of a string which is 0. Because strings start at zero

int startPosition = 0;

And our end position is going to be the length of the string. This is another method from the string class which will return the length of our string.

Here we have to do minus 1 to indicate the last character in the string.

int endPosition = str.length() - 1;

Now we also need a boolean variable to indicate whether our word is a palindrome or not

boolean checkPalindrome = true;

And now we can start our while loop which will continue as long as our start position is less than our end position.

Because the way we are going to implement this loop where we are going to compare the first letter to the last letter and continue there onwards.

Also Read – Palindrome number in java

So what I mean by that is, we are going to move our start position by 1 and decrement our end position by 1 with every single iteration of the loop.

In that way they meet in the center. But in order to properly test that our word is palindrome we are going to have to create a little substring variable that will test whether the two letters are actually equal.

So first we are going to create a string variable called “startLetter” which will hold our start letter which will be a substring of the initial palindrome variable.

Substring which will start at the start position and will end at the start position plus 1.

Same thing goes for variable “endLetter”. It will be a substring of the “str” variable and it will start at the end position and end at the end position plus 1.

So that way, since our end position is currently pointing to the last character our substring method will know to get the character that is starting at the last position all the way to the end of the string.

Also read – Palindrome string in java

Therefore during the first iteration we will get the letter “l” which is the last letter and during our start letter we also start with letter “l” in this case.

So all we have to do is to actually compare if they are equal by using the equals method dot equals the end letter.

But in this case what we are going to do is test if the equals method returns false.

If it does, we are going to break out of the loop and indicate that this word is not a palindrome by setting out “checkPalindrome” variable equal to false.

Here’s the while loop code for the above explanation,

while(startPosition < endPosition)
{
String startLetter = str.substring(startPosition, startPosition + 1);
String endLetter = str.substring(endPosition, endPosition + 1);

if(startLetter.equals(endLetter) == false)
{
checkPalindrome = false;
break;
}
startPosition++;
endPosition--;
}

In the end we are going to check out “checkPalindrome” variable equal to true. If it is true we know that our variable is palindrome.

Else we know that the variable must have been turned to false. Therefore it “must not be a palindrome”.

Here’s the java program to check whether given string is a palindrome,

public class PalindromeInJava
{
public static void main(String[] args)
{
String str = "level";
int startPosition = 0;
int endPosition = str.length() - 1;
boolean checkPalindrome = true;
while(startPosition < endPosition)
{
String startLetter = str.substring(startPosition, startPosition + 1);
String endLetter = str.substring(endPosition, endPosition + 1);
if(startLetter.equals(endLetter) == false)
{
checkPalindrome = false;
break;
}
startPosition++;
endPosition--;
}
if(checkPalindrome == true)
{
System.out.println(str + " is a palindrome.");
}
else
{
System.out.println(str + " is not a palindrome.");
}
}
}

Output :

level is a palindrome.


Note

There’s one more interesting case. What if we just capitalize one letter (in the above program change variable String str = “Level”)? Logically we know that this word is palindrome.

But does our program know that? Unfortunately it’s not !!! It’s not a palindrome.

What we need to do is we need to use another string method called “toLowerCase()”, like this

String strOriginal = "Level";
String str = strOriginal.toLowerCase();

There’s also another way to check whether our word is a palindrome without using substring method of the string class.

Also Read – How To Sort Hashmap By Values In Java

What we could do is assign the variable to a “char” variable using the chatAt() method at start position and we can also create another “char” variable for the end letter using the same logic.

char startLetter = str.charAt(startPosition);
char endLetter = str.charAt(endPosition);

Also we have to change the “if” condition to this,

if(startLetter != endLetter)

Here’s the complete code for palindrome program in java where one letter in a string is in uppercase,

public class PalindromeInJava
{
public static void main(String[] args)
{
String strOriginal = "Level";
String str = strOriginal.toLowerCase();
int startPosition = 0;
int endPosition = str.length() - 1;
boolean checkPalindrome = true;
while(startPosition < endPosition)
{
char startLetter = str.charAt(startPosition);
char endLetter = str.charAt(endPosition);
if(startLetter != endLetter)
{
checkPalindrome = false;
break;
}
startPosition++;
endPosition--;
}
if(checkPalindrome == true)
{
System.out.println(strOriginal + " is a palindrome.");
}
else
{
System.out.println(strOriginal + " is not a palindrome.");
}
}
}

Output:

Level is a palindrome.

Related Posts