Count palindrome words in a sentence | C ++ program to count number of 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool checkPalin ( string word ) { int n = word . length ( ) ; transform ( word . begin ( ) , word . end ( ) , word . begin ( ) , :: tolower ) ; for ( int i = 0 ; i < n ; i ++ , n -- ) if ( word . at ( i ) != word . at ( n - 1 ) ) return false ; return true ; } int countPalin ( string str ) { str = str + " ▁ " ; string word = " " ; int count = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { char ch = str . at ( i ) ; if ( ch != ' ▁ ' ) word = word + ch ; else { if ( checkPalin ( word ) ) count ++ ; word = " " ; } } return count ; } int main ( ) { cout << countPalin ( " Madam ▁ Arora ▁ teaches ▁ malayalam " ) << endl ; cout << countPalin ( " Nitin ▁ speaks ▁ malayalam " ) << endl ; }