Question
Frequency of Palindrome words in a given sentence using recursion.
ENTER THE SENTENCE
mom and dad are not at home
FREQUENCY OF PALINDROME WORDS IN GIVEN SENTENCE:2
Share code with your friends
Share on whatsapp
Share on facebook
Share on twitter
Share on telegram
Code
import java.util.Scanner;
public class FrequencyPalindromeWordsInSentence
{
public static String FindReverse(String wd,int i)
{
if(i>=0)
{
return wd.charAt(i)+ FindReverse(wd,i-1);
}
else
{
return "";
}
}
public static void main()
{
int i=0,count=0,len=0;
char ch=' ';
String sen="",word="";
Scanner sc=new Scanner(System.in);
System.out.println("ENTER THE SENTENCE");
sen=sc.nextLine();
sen=sen+" ";
len=sen.length();
for(i=0;i< len;i++)
{
ch=sen.charAt(i);
if(ch==' ')
{
if(word.equals(FindReverse(word,word.length()-1))==true)
{
count++;
}
word="";
}
else
{
word=word+ch;
}
}
System.out.println("FREQUENCY OF PALINDROME WORDS IN GIVEN SENTENCE:"+count);
}
}