Skip to content

Instantly share code, notes, and snippets.

@thmain
Created July 3, 2018 04:27
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 thmain/0f346d6503b7f9ed42c64c1ff0f85453 to your computer and use it in GitHub Desktop.
Save thmain/0f346d6503b7f9ed42c64c1ff0f85453 to your computer and use it in GitHub Desktop.
import java.util.Stack;
public class ReverseStringStack {
static void reverse(String input){
StringBuffer output = new StringBuffer();
if(input==null || input.isEmpty())
return;
Stack<Character> stack = new Stack<>();
for (int i = 0; i <input.length() ; i++) {
stack.add(input.charAt(i));
}
//traverse through stack, pop the characters and add it to StringBUffer
while (stack.empty()==false){
output.append(stack.pop());
}
System.out.println("Original String: " + input);
System.out.println("Reverse String: " + output);
}
public static void main(String[] args) {
String input = "tutorial horizon";
reverse(input);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment