Created
July 3, 2018 04:27
-
-
Save thmain/0f346d6503b7f9ed42c64c1ff0f85453 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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