Class Palindrome: 17 Dec, 2018 12:29:35 AM
Class Palindrome: 17 Dec, 2018 12:29:35 AM
/**
* 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;
}
int count=0;
System.out.print("OUTPUT : ");
for(int i=0; i<w; i++)
{
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
*/