Page 474 - CA_Blue( J )_Class10
P. 474
6 Scanner sc = new Scanner(System.in);
7 String str,word="",wordrev=""; //declaration of variable
8 int len,i,j;
9 char ch;
10 System.out.print("Enter a sentence : ");
11 str = sc.nextLine(); //Enter of sentence
12 str = str + " ";
13 len = str.length(); //Length of sentence
14 for (i = 0; i < len; i++)
15 {
16 ch = str.charAt(i); //Extraction of character
17 if (ch != ‘ ‘) // checking for space
18 {
19 word=word+ch; //Extraction of word
20 wordrev=ch+wordrev; //Extracting and reversing the word
21 }
22 else
23 {
24 if(word.equalsIgnoreCase(wordrev)) //Checking whether both the
words are equal
25 {
26 System.out.println(word); //Printing the palindromic
word
27 }
28 word = "";
29 wordrev="";
30
31 }
32 }
33 } //close of main()
34 } //close of class
Output
Enter a sentence : MOM AND DAD WENT TO MARKET
MOM
DAD
472472 Touchpad Computer Applications-X

