Reverse the Words of a String using Stack | C # implementation of the above approach ; Function to reverse the words of the given String without using strtok ( ) . ; Create an empty String stack ; Create an empty temporary String ; Traversing the entire String ; Push the temporary variable into the stack ; Assigning temporary variable as empty ; For the last word of the String ; Get the words in reverse order ; Driver code
using System ; using System . Collections ; class GFG { static void reverse ( string s ) { Stack stc = new Stack ( ) ; string temp = " " ; for ( int i = 0 ; i < s . Length ; i ++ ) { if ( s [ i ] == ' ▁ ' ) { stc . Push ( temp ) ; temp = " " ; } else { temp = temp + s [ i ] ; } } stc . Push ( temp ) ; while ( stc . Count != 0 ) { temp = ( string ) stc . Peek ( ) ; Console . Write ( temp + " ▁ " ) ; stc . Pop ( ) ; } Console . WriteLine ( ) ; } static void Main ( ) { string s = " I ▁ Love ▁ To ▁ Code " ; reverse ( s ) ; } }