0% found this document useful (0 votes)
27 views2 pages

Class Palindrome: 17 Dec, 2018 12:29:35 AM

This Java program accepts a sentence in uppercase as input and identifies palindromic words in the sentence. It uses a StringTokenizer to separate words and a recursive method to check if a word is a palindrome by comparing it to its reverse. It outputs the number of palindromic words found and lists those words.

Uploaded by

Seema Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views2 pages

Class Palindrome: 17 Dec, 2018 12:29:35 AM

This Java program accepts a sentence in uppercase as input and identifies palindromic words in the sentence. It uses a StringTokenizer to separate words and a recursive method to check if a word is a palindrome by comparing it to its reverse. It outputs the number of palindromic words found and lists those words.

Uploaded by

Seema Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Class Palindrome 1/2

/**
* A Palindrome is a word that may be read the same way in either direction.
* Accept a sentence in UPPER CASE which is terminated by either ” . “, ” ?
” or ” ! “.
* Each word of the sentence is separated by a single blank space. Perform t
he following tasks:
* (a) Display the count of palindromic words in the sentence.
* (b) Display the Palindromic words in the sentence.
*/

import java.io.*;
import java.util.*;
class Palindrome
{
static BufferedReader br=new BufferedReader (new InputStreamReader (Syst
em.in));
boolean isPalin(String s)
{
int l=s.length();
String rev="";
for(int i=l-1; i>=0; i--)
{
rev=rev+s.charAt(i);
}
if(rev.equals(s))
return true;
else
return false;
}

public static void main(String args[])throws IOException


{
Palindrome ob=new Palindrome();
System.out.print("Enter any sentence : ");
String s=br.readLine();
s=s.toUpperCase();

StringTokenizer str = new StringTokenizer(s,".?! ");


int w=str.countTokens();

String word[]=new String[w];


for(int i=0;i<w;i++)
{
word[i]=str.nextToken();
}

int count=0;
System.out.print("OUTPUT : ");
for(int i=0; i<w; i++)

17 Dec, 2018 12:29:35 AM


Class Palindrome (continued) 2/2

{
if(ob.isPalin(word[i])==true)
{
count++;
System.out.print(word[i]+" ");
}
}

if(count==0)
System.out.println("No Palindrome Words");
else
System.out.println("Number of Palindromic Words : "+count);
}
}
/** OUTPUT
* Enter any sentence : REFER TO MADAM JALAJ IN THE NOON FOR RACECAR.
OUTPUT : REFER MADAM JALAJ NOON RACECAR Number of Palindromic Words : 5
Enter any sentence : ROHIT WAS ON RADAR FOR CIVIC AUTHORISATION.
OUTPUT : RADAR CIVIC Number of Palindromic Words : 2
*/

17 Dec, 2018 12:29:35 AM

You might also like