Java program to check whether a string is a palindrome.

Saraswathi Rajkumar
4 min readApr 29, 2024

--

In our previous post, we discussed a program to check if a given number is a palindrome or not. In this post, we are going to check if a given string is a palindrome or not using different methods.

Palindrome: When a word, sentence, or number is read backward or forward, then that value must be the same.

How to check a string is palindrome:

Either by comparing the given string and its reverse, or by checking each character in the string starting with the first and last char, then checking the second and the char before the last.

In different ways, we can get results:

1. Using a loop

2. Using the built-in function reverse()

3. Using the recursion concept

4. Compare characters in a string

public class CheckStringisPalindrome 
{
//Palindrome-When read word,sentence or number from backward or forward then that value must same.
//upper and lower case letters doesn't matter.so convert string into upper/lower case.
//how to do?
//1.compare given string & its reverse
//2.check character in string-from first & last char then check second and char before last one like that.

//We write program by different methods:
//1.using loop
//2.using build in function-reverse()
//3.using recursion concept
//4.compare characters in string
public static void checkPalindrome(String str)
{
//convert string into lower case
String word=str.toLowerCase();
//variable-revStr-to combine each iteration results.so,create variable as empty string.
String revStr="";
//using loop iterate each char from string-we need reverse of string-so start with last char until first char in string
for(int i=word.length()-1;i>=0;i--)
{
//get char from given index
char ch=word.charAt(i);
//concate char with string
revStr+=ch;
}
//check given string and reversed string are same
if(word.equals(revStr))
{
//print palindrome
System.out.println("palindrome");
}
//both the string not equal
else
{
//print not palindrome
System.out.println("not palindrome");
}
}
public static void checkPalindrome1(String str)
{
//to use built-in function to convert string into string builder/string buffer
StringBuilder word=new StringBuilder(str);
//reverse string using reverse()
StringBuilder revWord=word.reverse();
//convert reversed string in string builder format to string format
String reversedString=revWord.toString();
//convert string into lowercase-to avoid case sensitive issue
str=str.toLowerCase();
//check given string and reversed string are same
if(str.equals(reversedString))
{
//print palindrome
System.out.println("palindrome");
}
//both the string not equal
else
{
//print not palindrome
System.out.println("not palindrome");
}

}
public static void checkPalindrome2(String word)
{
//convert string into lowercase-to avoid case sensitive issue
String str=word.toLowerCase();
//call usingRecursion()-to reverse string
String reversedString=usingRecursion(word,"");
//check given string and reversed string are same
if(str.equals(reversedString))
{
//print palindrome
System.out.println("palindrome");
}
//both the string not equal
else
{
//print not palindrome
System.out.println("not palindrome");
}

}
//function to reverse string
public static String usingRecursion(String str,String rev)
{
//convert string into lowercase and check length of string is '0'
if((str.toLowerCase()).length()==0)
{
//return reversed string
return rev;
}
//if string length!=0
else
{
//first retrieve char from and concat with rev variable
//start getting char from last index
//e.g str=code
//first time-rev=""+e->e
//2nd time-rev=e+d->ed
rev+=str.substring(str.length()-1);
//call the usingRecursion() with argument-pass the string (contains chars-expect char we retrieve in previous step)
//substring(0,str.length()-1)-starting from 0th index,exclude char in str.length()-1
//pass rev-to combine results
//e.g ist time-usingRecursion(str.substring(0,3),e)->usingRecursion("cod","e");
//2nd time-usingRecursion(str.substring(0,2),ed)->usingRecursion("co","ed");
//3rd time-usingRecursion(str.substring(0,1),edo)->usingRecursion("c","edo");
//4th time-usingRecursion(str.substring(0,0),edoc)->usingRecursion("","edoc");
return usingRecursion((str.substring(0,str.length()-1)),rev);
}
}
public static void checkfirstLastIndex(String str)
{
//convert string into lower case
String word=str.toLowerCase();
//Initialize variable i with-0-get first char in string
int i=0;
//Initialize variable j with-word.length()-1-get char from last index of string
int j=word.length()-1;
//create variable-result-to store status of result
boolean match=false;
//continue the loop until i<j-check the chars in both side -no element to compare-stop the loop
//e.g-bob-1st iteration-0<2,2nd iteration-1<1,condition false-no element to check
while(i<j)
{
//e.g:bob->rev->bob
//how to verify-check first char with last char in string-b=b
if(word.charAt(i)==word.charAt(j))
{
//if both chars are same then set match=true
match=true;
}
//otherwise,set match=false
//even one char doesn't match- then it is not palindrome and break the loop
else
{
match=false;
break;
}
//increment i value-to get next char for next iteration
i++;
//decrement j value-to get previous char for next iteration
j--;
}
//if match variable is true
if(match)
{
//print palindrome
System.out.println("palindrome");
}
//otherwise
else
{
//print not palindrome
System.out.println("not palindrome");
}
}
public static void main(String[] args) {
//call the methods
checkPalindrome("dad");
checkPalindrome1("CAt");
checkPalindrome2("code");
checkfirstLastIndex("abc");
}

}
Output:
palindrome
not palindrome
not palindrome
not palindrome

If you wish to read important Java codes for an interview, kindly check the link.

Thanks for reading.

--

--

Saraswathi Rajkumar

I like to share contents related to automation testing, manual testing and java .