Modify a sentence by reversing order of occurrences of all Palindrome Words | C # implementation of the above approach ; Function to check if a string S is a palindrome ; Function to print the modified string after reversing teh order of occurrences of all palindromic words in the sentence ; Stores the palindromic words ; Stores the words in the list ; Traversing the list ; If current word is a palindrome ; Update newlist ; Reverse the newlist ; Traverse the list ; If current word is a palindrome ; Update lis [ i ] ; Increment j ; Print the updated sentence ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static bool palindrome ( string str ) { int st = 0 ; int ed = str . Length - 1 ; while ( st < ed ) { if ( str [ st ] == str [ ed ] ) { st ++ ; ed -- ; } else return false ; } return true ; } static void printReverse ( string sentence ) { List < string > newlist = new List < string > ( ) ; List < string > lis = new List < string > ( ) ; string temp = " " ; foreach ( char x in sentence ) { if ( x == ' ▁ ' ) { lis . Add ( temp ) ; temp = " " ; } else temp += x ; } lis . Add ( temp ) ; foreach ( string x in lis ) { if ( palindrome ( x ) ) newlist . Add ( x ) ; } newlist . Reverse ( ) ; int j = 0 ; for ( int i = 0 ; i < lis . Count ; i ++ ) { if ( palindrome ( lis [ i ] ) ) { lis [ i ] = newlist [ j ] ; j = j + 1 ; } } foreach ( string x in lis ) Console . Write ( x + " ▁ " ) ; } public static void Main ( ) { string sentence = " mom ▁ and ▁ dad ▁ went ▁ to ▁ eye ▁ hospital " ; printReverse ( sentence ) ; } }