Reverse words in a given string | Set 2 | C # program to implement the above approach ; Function to reverse the words of a given String ; Stack to store each word of the String ; Store the whole String in String stream ; Push each word of the String into the stack ; Print the String in reverse order of the words ; Driver Code
using System ; using System . Collections ; class GFG { static void printRev ( string str ) { Stack st = new Stack ( ) ; String [ ] separator = { " ▁ " } ; string [ ] ss = str . Split ( separator , int . MaxValue , StringSplitOptions . RemoveEmptyEntries ) ; foreach ( string temp in ss ) { st . Push ( temp ) ; } while ( st . Count > 0 ) { Console . Write ( st . Peek ( ) + " ▁ " ) ; st . Pop ( ) ; } } public static void Main ( string [ ] args ) { string str ; str = " geeks ▁ quiz ▁ practice ▁ code " ; printRev ( str ) ; } }