forked from hacktoberfest17/programming
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a program sentence_Palindrome.java
The program prints the palindrome words present in the sentence and its frequency.
- Loading branch information
1 parent
2e91545
commit d206f7e
Showing
1 changed file
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |