-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathPallinCount.java
More file actions
52 lines (45 loc) · 1.03 KB
/
Copy pathPallinCount.java
File metadata and controls
52 lines (45 loc) · 1.03 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
/*Java program to count number of palindrome
words in a sentence*/
class PallinCount {
// Function to check if a word is
// palindrome
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;
}
// Function to count palindrome words
static int countPalin(String str)
{
// to check last word for palindrome
str = str + " ";
// to store each word
String word = "";
int count = 0;
for (int i = 0; i < str.length(); i++)
{
char ch = str.charAt(i);
// extracting each word
if (ch != ' ')
word = word + ch;
else {
if (checkPalin(word))
count++;
word = "";
}
}
return count;
}
// Driver code
public static void main(String args[])
{
System.out.println(countPalin("Madam "
+ "Arora teaches malayalam"));
System.out.println(countPalin("Nitin "
+ "speaks malayalam"));
}
}