Page 417 - Cs_withBlue_J_C11_Flipbook
P. 417
Program 4 Define a class called Palword to check if a word is a palindrome or not.
A palindrome word is a word which can be read same from both ends. For example, NOON,
MADAM, MALAYALAM, etc. The detail of the class is given as follows:
Class name : Palword
Data Members
String w : To store word
String rw : To store the reverse string
Member Methods
void read() : Accepts any word in uppercase
String revword(String x) : Using recursive technique, returns the reverse of the word
void check() : Calls revword(String) and prints if w is a palindrome or not
static void main() : Creates object and calls other methods
1 import java.util.*;
2 class Palword
3 {
4 String w,rw;
5 void read() // input
6 {
7 Scanner sc=new Scanner(System.in);
8 System.out.println("Enter word in uppercase");
9 w=sc.next();
10 rw="";
11 }
12 String revword(String x)
13 {
14 if(x.equals("")) //base case
15 return(rw);
16 else
17 {
18 rw=x.charAt(0)+rw;// finding reverse word
19 return revword(x.substring(1)); //recursive case
20 }
21 }
22 void check()
23 {
24 if(revword(w).equals(w))// checking word=reverse word
415
Recursion 415

