Count palindrome words in a sentence | Function to check if a word is palindrome ; Function to count palindrome words ; splitting each word as spaces as delimiter and storing it into a list ; Iterating every element from list and checking if it is a palindrome . ; if the word is a palindrome increment the count . ; Driver code
def checkPalin ( word ) : NEW_LINE INDENT if word . lower ( ) == word . lower ( ) [ : : - 1 ] : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT def countPalin ( str ) : NEW_LINE INDENT count = 0 NEW_LINE listOfWords = str . split ( " ▁ " ) NEW_LINE for elements in listOfWords : NEW_LINE INDENT if ( checkPalin ( elements ) ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT print ( count ) NEW_LINE DEDENT countPalin ( " Madam ▁ Arora ▁ teaches ▁ malayalam " ) NEW_LINE countPalin ( " Nitin ▁ speaks ▁ malayalam " ) NEW_LINE