Print longest palindrome word in a sentence | Java 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
public class GFG { static boolean checkPalin ( String word ) { int n = word . length ( ) ; word = word . toLowerCase ( ) ; for ( int i = 0 ; i < n ; i ++ , n -- ) if ( word . charAt ( i ) != word . charAt ( 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 . charAt ( 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 args [ ] ) { String s = new String ( " My ▁ name ▁ is ▁ ava ▁ " + " and ▁ i ▁ love ▁ Geeksforgeeks " ) ; if ( longestPalin ( s ) == " " ) System . out . println ( " No ▁ Palindrome " + " ▁ Word " ) ; else System . out . println ( longestPalin ( s ) ) ; } }