Print longest palindrome word in a sentence | C ++ program to print longest palindrome word in a sentence and its length ; Function to check if a word is palindrome ; making the check case case insensitive word = word . toLowerCase ( ) ; ; loop to check palindrome ; Function to find longest palindrome word ; to check last word for palindrome ; to store each word ; extracting each word ; Driver code
#include <iostream> NEW_LINE #include <algorithm> NEW_LINE #include <string> 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 [ i ] != word [ n - 1 ] ) return false ; return true ; } string longestPalin ( string str ) { str = str + " ▁ " ; string longestword = " " , word = " " ; int length , length1 = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { char ch = str [ i ] ; if ( ch != ' ▁ ' ) word = word + ch ; else { length = word . length ( ) ; if ( checkPalin ( word ) && length > length1 ) { length1 = length ; longestword = word ; } word = " " ; } } return longestword ; } int main ( ) { string s = " My ▁ name ▁ is ▁ ava ▁ and ▁ i ▁ love " " ▁ Geeksforgeeks " ; if ( longestPalin ( s ) == " " ) cout << " No ▁ Palindrome " << " ▁ Word " ; else cout << longestPalin ( s ) ; return 0 ; }