Remove all the palindromic words from the given sentence | Java implementation to remove all the palindromic words from the given sentence ; function to check if ' str ' is palindrome ; traversing from both the ends ; not palindrome ; palindrome ; function to remove all the palindromic words from the given sentence ; ' final _ str ' to store the final string and ' word ' to one by one store each word of ' str ' ; add space at the end of ' str ' ; traversing ' str ' ; accumulating characters of the current word ; if ' word ' is not palindrome then a add it to ' final _ str ' ; reset ; required final string ; Driver code
class GFG { static boolean isPalindrome ( String str ) { int i = 0 , j = str . length ( ) - 1 ; while ( i < j ) { if ( str . charAt ( i ++ ) != str . charAt ( j -- ) ) return false ; } return true ; } static String removePalinWords ( String str ) { String final_str = " " , word = " " ; str = str + " ▁ " ; int n = str . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( str . charAt ( i ) != ' ▁ ' ) word = word + str . charAt ( i ) ; else { if ( ! ( isPalindrome ( word ) ) ) final_str += word + " ▁ " ; word = " " ; } } return final_str ; } public static void main ( String [ ] args ) { String str = " Text ▁ contains ▁ malayalam ▁ and ▁ level ▁ words " ; System . out . print ( removePalinWords ( str ) ) ; } }