Find The Number Of Sentences That Have 2 Or More Palindrome Words In It
WAP to enter a paragraph which may be terminated by a '?', '.' or an '!'. Print the number of sentences that have 2 or more palindrome words in it.
/*
* WAP to enter a paragraph which may be terminated by a '?', '.' or an '!'.
* Print the number of sentences that have 2 or more palindrome words in it.
*/
import java.io.*;
class NoOfPalindromes
{
public static void main (String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader (System.in));
int i,p,j,f,l,count, nos=0;
String str, word, revWord;
char ch, ch1;
System.out.print("Enter a paragraph(terminated by '?', '.' or '!') : ");
str = br.readLine();
l= str.length();
p=0;
for(i=0;i< l;i++)
{
ch=str.charAt(i);
// check for the end of a sentence
if(ch=='!' ||ch=='?'|| ch=='.')
{
f=0;
count=0;
//Loop to traverse through each sentence
for(j=p;j< i+1;j++)
{
ch1=str.charAt(j);
//Extract a word
if(ch1==' '||ch1=='!' ||ch1=='?'|| ch1=='.')
{
word=str.substring(f,j);
//Reverse the word
revWord = (new StringBuffer(word).reverse()).toString();
//Count palindrome words
if(word.equals(revWord))
count++;
f=j+1;
}
}
// If the sentence has two or more palindrome words increment
// the counter
if(count>=2)
nos++;
p=i+1;
}
}
System.out.println("Number of sentences with two or more palindrome
words in it : "+nos);
}//end of main
}//end of class
Have something to say? Log in to comment on this post.
0 comments