-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpalindrome_string.java
More file actions
54 lines (48 loc) · 901 Bytes
/
palindrome_string.java
File metadata and controls
54 lines (48 loc) · 901 Bytes
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
/*
Input -> Mom and Dad are my best friends
Output -> 2
*/
import java.util.*;
class Main
{
static int countPalindrome(String str)
{
str=str+" ";
String word="";
int i,count=0;
for(i=0;i<str.length();i++)
{
char ch=str.charAt(i);
if(ch!=' ')
word=word+ch;
else
{
if(checkPalin(word))
count++;
word="";
}
}
return count;
}
static boolean checkPalin(String word)
{
int i;
int n=word.length();
word=word.toLowerCase();
for(i=0;i<n;i++,n--)
{
if(word.charAt(i)!=word.charAt(n-1))
return false;
}
return true;
}
public static void main(String[] args)
{
int j=0;
String a;
Scanner s=new Scanner(System.in);
a = s.nextLine().trim();
j=countPalindrome(a);
System.out.println(j);
}
}