0
1 answers
0
import java.io.*;
import java.util.*;
public class ISC2013
{
public void main()throws IOException
{
InputStreamReader read=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(read);
System.out.println("INPUT :");
String s=in.readLine().toUpperCase();
int l=s.length(),i=1;String Str="";
StringTokenizer st=new StringTokenizer(s,".!?");
int c=st.countTokens();
while(st.hasMoreTokens())
{
Str=st.nextToken();
}
StringTokenizer stk=new StringTokenizer(Str," "); String s3[]=new String[c];
int c1=stk.countTokens();String m="";
while(stk.hasMoreTokens())
{
String s2=stk.nextToken();
StringBuffer sb=new StringBuffer(s2);
int l1=s2.length();
String r=sb.reverse().toString();
if(r.equalsIgnoreCase(s2))
{
i++;
System.out.println(i);
}
}
}
}
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 the following taks:
(a) display the count of palindromic words in the sentence.
(b) Display the palindromic words in the sentence.
Example of palindromic words:
MADAM, ARORA, NOON
Test your program with the sample data and some random data:
Example 1
INPUT : MOM AND DAD ARE COMING AT NOON
OUTPUT : MOM DAD NOON
NUMBER OF PALINDROMIC WORDS : 3
Example 2
INPUT : HOW ARE YOU?
OUTPUT : NO PALINDROMIC WORDS
Answer :
import java.io.*;
import java.util.*;
public class ISC2013
{
public void main()throws IOException
{
InputStreamReader read=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(read);
System.out.println("INPUT :");
String s=in.readLine().toUpperCase();
int l=s.length(),i=0;String Str="";
StringTokenizer st=new StringTokenizer(s,". ");
while(st.hasMoreTokens())
{
String s2=st.nextToken();
StringBuffer sb=new StringBuffer(s2);
int l1=s2.length();
String r=sb.reverse().toString();
if(r.equalsIgnoreCase(s2))
{
i++;
System.out.println(s2);
}
}
if(i>0)
{
System.out.println("The total number of palindrome words found : " +i);
}
else
{
System.out.println("There are no palindrome words");
}
}
}
Log in to your account to answer this questions.