text: Remove all the palindromic words from the given sentence | C ++ 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 program to test above
code: #include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPalindrome ( string str ) { int i = 0 , j = str . size ( ) - 1 ; while ( i < j ) if ( str [ i ++ ] != str [ j -- ] ) return false ; return true ; } string removePalinWords ( string str ) { string final_str = " " , word = " " ; str = str + " ▁ " ; int n = str . size ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( str [ i ] != ' ▁ ' ) word = word + str [ i ] ; else { if ( ! ( isPalindrome ( word ) ) ) final_str += word + " ▁ " ; word = " " ; } } return final_str ; } int main ( ) { string str = " Text ▁ contains ▁ malayalam ▁ and ▁ level ▁ words " ; cout << removePalinWords ( str ) ; return 0 ; }