Print longest palindrome word in a sentence | Function to check if a word is palindrome ; making the check case case insensitive ; 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
def checkPalin ( word ) : NEW_LINE INDENT n = len ( word ) NEW_LINE word = word . lower ( ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( word [ i ] != word [ n - 1 ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT n -= 1 NEW_LINE DEDENT return True NEW_LINE DEDENT def longestPalin ( str ) : NEW_LINE INDENT str = str + " ▁ " NEW_LINE longestword = " " NEW_LINE word = " " NEW_LINE length1 = 0 NEW_LINE for i in range ( len ( str ) ) : NEW_LINE INDENT ch = str [ i ] NEW_LINE if ( ch != ' ▁ ' ) : NEW_LINE INDENT word = word + ch NEW_LINE DEDENT else : NEW_LINE INDENT length = len ( word ) NEW_LINE if ( checkPalin ( word ) and length > length1 ) : NEW_LINE INDENT length1 = length NEW_LINE longestword = word NEW_LINE DEDENT word = " " NEW_LINE DEDENT DEDENT return longestword NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = " My ▁ name ▁ is ▁ ava ▁ and ▁ i ▁ love ▁ Geeksforgeeks " NEW_LINE if ( longestPalin ( s ) == " " ) : NEW_LINE INDENT print ( " No ▁ Palindrome ▁ Word " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( longestPalin ( s ) ) NEW_LINE DEDENT DEDENT