forked from divScorp/Java-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsentencePalindrome.java
More file actions
61 lines (57 loc) · 1.45 KB
/
sentencePalindrome.java
File metadata and controls
61 lines (57 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*
Print the palindrome words present in the sentence and its frequency.
*/
import java.util.*;
class sentencePalindrome
{
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");
}
}