Reverse individual words | C # program to reverse individual words in a given string using STL list ; reverses individual words of a string ; Traverse given string and push all characters to stack until we see a space . ; When we see a space , we print contents of stack . ; Since there may not be space after last word . ; Driver Code
using System ; using System . Collections . Generic ; class GFG { public static void reverseWords ( string str ) { Stack < char > st = new Stack < char > ( ) ; for ( int i = 0 ; i < str . Length ; ++ i ) { if ( str [ i ] != ' ▁ ' ) { st . Push ( str [ i ] ) ; } else { while ( st . Count > 0 ) { Console . Write ( st . Pop ( ) ) ; } Console . Write ( " ▁ " ) ; } } while ( st . Count > 0 ) { Console . Write ( st . Pop ( ) ) ; } } public static void Main ( string [ ] args ) { string str = " Geeks ▁ for ▁ Geeks " ; reverseWords ( str ) ; } }