-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11 Oct 2022.java
64 lines (47 loc) · 1.06 KB
/
11 Oct 2022.java
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
62
63
64
// Problem Statement : 11 Oct 2022
// Accept a string from user and print the total number of pallindrome word
// Input: "MOM AND DAD ARE MY BEST FRIENDS."
// Output : 2
// Input : str1 = "ABCBA ABCD DBBD BCDCB"
// Output : 3
// Input : str1 = ""
// Output : 0
// Input : str1 = "I like to play Cricket"
// Output : 1
// Solution:-
import java.util.Scanner;
class Main{
static boolean checkPalin(String word)
{
int n = word.length();
word = word.toLowerCase();
for (int i=0; i<n; i++,n--)
if (word.charAt(i) != word.charAt(n - 1))
return false;
return true;
}
static int countPalin(String str)
{
str = str + " ";
String word = "";
int count = 0;
for (int i = 0; i < str.length(); i++)
{
char ch = str.charAt(i);
if (ch != ' ')
word = word + ch;
else {
if (checkPalin(word))
count++;
word = "";
}
}
return count;
}
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
String firstword = sc.nextLine();
System.out.println(countPalin(firstword));
}
}