Count palindrome words in a sentence | Function to check if a word is palindrome ; Function to count palindrome words ; to check last word for palindrome ; to store each word ; extracting each word ; Driver code
function checkPalin ( word ) { var n = word . length word = word . toLowerCase ( ) ; for ( var i = 0 ; i < n ; i ++ , n -- ) if ( word [ i ] != word [ n - 1 ] ) return false ; return true ; } function countPalin ( str ) { str = str + " " ; var word = " " ; var count = 0 ; for ( var i = 0 ; i < str . length ; i ++ ) { var ch = str [ i ] ; if ( ch != ' ' ) word = word + ch ; else { if ( checkPalin ( word ) ) count ++ ; word = " " ; } } return count ; } document . write ( countPalin ( " " ) + " " ) ; document . write ( countPalin ( " " ) ) ;