Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a program sentence_Palindrome.java #1

Merged
merged 1 commit into from
Oct 2, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions sentence_Palindrome.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
Print the palindrome words present in the sentence and its frequency.
*/


import java.util.*;
class sentence_Palindrome
{
String str;
void accept()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a sentence");
str=sc.nextLine();
str=str.toUpperCase();
}

boolean isPalin(String s)
{//checks if the word is Palindrome or not
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;
}

void main()
{
accept();
char ch=str.charAt(str.length()-1);
if(ch=='.' || ch=='!' || ch=='?')
{
int freq=0;
StringTokenizer st=new StringTokenizer(str," .!?");
int c=st.countTokens();
for(int i=1; i<=c; i++)
{
String w=st.nextToken();
boolean r=isPalin(w);
if (r==true)
{
System.out.print(w+" ");
freq++;
}
}
System.out.println();
if(freq!=0)
System.out.println("NUMBER OF PALINDROMIC WORDS =”+ freq);
else
System.out.println("NO PALINDROMIC WORDS");
}
}
else
System.out.println("INVALID INPUT");
}
}
© 2020 GitHub, Inc.