ISC Practicals and Theory - Palindrome String (Recursive Technique) (OOP)
Updated 20 Oct 2026
This version checks whether a word is a palindrome using recursion, written as a class whose object is built with a parameterised constructor. The word is passed to the object when it is created, and each method does one job: test, store the answer, print.
Definition
A string is a palindrome if it reads the same from left to right and from right to left, for example racecar. A string of length 0 or 1 is always a palindrome, so a one-character word such as x is reported as a palindrome (checked by running the program). Capital and small letters are treated as equal because the constructor converts the word to lower case, so Aa is a palindrome.
The word is read with next(), so only the first word typed is tested (for "nurses run" only nurses is tested), and spaces and punctuation are not handled. An empty input cannot be tested: next() finds no word to read and the program stops with a NoSuchElementException.
Algorithm
mainreads a word and creates the objectobwithnew PalindromeOOP(w). The constructor storesw.toLowerCase()inwordand setsresulttofalse.check()callsisPalin(word)and keeps the answer inresult.isPalin(s)is the recursive method:- Find
n, the length ofs. - Base case: if
n <= 1, returntrue. - If
s.charAt(0)ands.charAt(n - 1)differ, returnfalse. - Recursive case: return
isPalin(s.substring(1, n - 1)), the same test without the first and last characters.
- Find
display()prints "is a Palindrome" ifresultistrue, otherwise "is not a Palindrome".
Program
The class PalindromeOOP has two data members, a parameterised constructor, the recursive isPalin(), and the one-job methods check() and display(). main reads the word with Scanner, creates the object and calls the methods.
import java.util.Scanner;
/* Class PalindromeOOP checks recursively whether a word is a palindrome.
The word is given to the constructor when the object is created. */
public class PalindromeOOP
{
String word; // the word to be tested (stored in lower case)
boolean result; // true if word is a palindrome, false otherwise
/* Parameterised constructor. Parameter w: the word typed by the user.
Stores it in lower case so that upper/lower case are treated alike. */
PalindromeOOP(String w)
{
word = w.toLowerCase();
result = false;
}
/* Recursive method. Parameter s: the part of the word still to be checked.
Returns true if s is a palindrome, otherwise false. */
boolean isPalin(String s)
{
int n = s.length(); // length of the current string
if (n <= 1) // base case: nothing left to compare
return true;
if (s.charAt(0) != s.charAt(n - 1)) // end characters differ
return false;
return isPalin(s.substring(1, n - 1)); // recursive case: remove both ends
}
/* Calls isPalin() on the stored word and keeps the answer in result */
void check()
{
result = isPalin(word);
}
/* Prints the message according to the value of result */
void display()
{
if (result)
System.out.println(word + " is a Palindrome");
else
System.out.println(word + " is not a Palindrome");
}
/* main reads the word, creates the object and calls the methods */
public static void main(String args[])
{
Scanner sc = new Scanner(System.in); // reads input from the keyboard
System.out.print("Enter a word: ");
String w = sc.next(); // word typed by the user
PalindromeOOP ob = new PalindromeOOP(w); // object of the class
ob.check();
ob.display();
}
}Variable description table
| Variable | Data Type | Purpose |
|---|---|---|
word | String | Data member: the word to be tested, in lower case |
result | boolean | Data member: true if word is a palindrome |
w | String | Parameter of the constructor and local in main: the word as typed |
s | String | Parameter of isPalin(): the part of the word still to be checked |
n | int | Local in isPalin(): length of s |
sc | Scanner | Reads input from the keyboard in main |
args | String[] | Command line arguments of main (not used) |
ob | PalindromeOOP | Object created in main to call the methods |
Sample input and output
Sample 1
Enter a word: Racecar
racecar is a PalindromeSample 2
Enter a word: Abca
abca is not a PalindromeSample 3
Enter a word: x
x is a PalindromeDry run
Input Racecar. The constructor stores word = racecar; check() calls isPalin("racecar").
| Call | s | n | First and last | Action |
|---|---|---|---|---|
| 1 | racecar | 7 | r, r (equal) | calls isPalin("aceca") |
| 2 | aceca | 5 | a, a (equal) | calls isPalin("cec") |
| 3 | cec | 3 | c, c (equal) | calls isPalin("e") |
| 4 | e | 1 | not compared | base case, returns true |
| back in 3, 2, 1 | each returns true |
result becomes true, and display() prints racecar is a Palindrome, the same as the real run above.
Variations
- Palindrome sentence "Write a program to check whether a sentence is a palindrome, ignoring spaces and punctuation."
- Reverse a string "Write a program to reverse a string using recursive technique."