Skip to content

Instantly share code, notes, and snippets.

@gdyrrahitis
Last active November 9, 2018 19:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gdyrrahitis/92dd59993d1cc4b08797db6a9fd3fa2d to your computer and use it in GitHub Desktop.
Save gdyrrahitis/92dd59993d1cc4b08797db6a9fd3fa2d to your computer and use it in GitHub Desktop.
public static class Reverser
{
public static string Reverse(string word)
{
// Create the stack
var stack = new Stack<char>();
// Insert each character from the string input in the stack
foreach (var character in word)
{
stack.Push(character);
}
// Pop characters from the stack
// and store in a StringBuilder
var builder = new StringBuilder();
while (!stack.IsEmpty)
{
builder.Append(stack.Pop());
}
return builder.ToString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment