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 ; 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
using System ; class GFG { static bool checkPalin ( string word ) { int n = word . Length ; word = word . ToLower ( ) ; for ( int i = 0 ; i < n ; i ++ , n -- ) if ( word [ i ] != word [ n - 1 ] ) return false ; return true ; } static 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 ; } public static void Main ( ) { string s = " My ▁ name ▁ is ▁ ava ▁ and ▁ i " + " ▁ love ▁ Geeksforgeeks " ; if ( longestPalin ( s ) == " " ) Console . Write ( " No ▁ Palindrome ▁ Word " ) ; else Console . Write ( longestPalin ( s ) ) ; } }