Monday, December 31, 2018

Java program to count number of palindrome words in a given sentence



package example;

import java.util.StringTokenizer;

public class palindromeCount {

public static void main(String[] args) {
String str = " Madam Arora non teaches io malayalam ";
StringTokenizer st = new StringTokenizer(str, " ");
int palinCount = 0;

while (st.hasMoreElements()) {
if (checkPalindrome(st.nextToken())) {
palinCount++;
}
}
System.out.println("palindrome words count:" + palinCount);

}

private static boolean checkPalindrome(String str) {
StringBuilder sb = new StringBuilder();
sb.append(str);
if (str.equalsIgnoreCase(sb.reverse().toString())) {
return true;
} else
return false;

}

}

Output:    palindrome words count:4

No comments:

Post a Comment