using System.Collections.Generic;
public static void Main()
string str = "My Name is khan. What's is your name?";
Console.WriteLine(ReverseWord(str));
Console.WriteLine(ReverseAWordUsingStack(str));
public static string ReverseWord(string str)
for(var i=0; i < str.Length; i++)
str = Reverse(str, lastIndex, i-1);
str = Reverse(str, lastIndex, str.Length-1);
str = Reverse(str, 0, str.Length-1);
public static string Reverse(string str, int startIndex, int endIndex){
char[] strChar = str.ToCharArray();
while(startIndex < endIndex)
tempChar = strChar[startIndex];
strChar[startIndex] = strChar[endIndex];
strChar[endIndex] = tempChar;
return new String(strChar);
public static string ReverseAWordUsingStack(string str){
Stack<string> stack = new Stack<string>();
for(var i=0; i < str.Length; i++)
tempStr += str[i].ToString();
string reverseString = "";
reverseString += stack.Pop();
if(stack.Count > 0) reverseString += " ";