Why does this code does not work?
str get "abc redder noon" and function should return count Word == palindrome
in this case, I should return 2!;
public static int countPolindroms(String str) {
int countWords = 0, start = 0, last, i;
boolean flag = true;
str=str.toUpperCase();
for (i = 0; i < str.length(); i++) {
if (str.charAt(i) == ' ' || i == str.length() - 1) {
if (i != str.length() - 1)
last = i - 1;
else
last = i;
for ( ;start < last && flag ; start++, last--) {
if(str.charAt(start) != str.charAt(last))
flag = false;
}
if (flag)
countWords++;
if (str.charAt(i) == ' ')
start = i + 1;
}
}
return countWords;
}