One use of a Stack is to reverse the order of input Write a
Solution
StringStackTest.java
import java.util.Scanner;
import com.examples.ArrayStack;
public class StringStackTest {
public static void main(String[] args) {
readStrings();
}
public static void readStrings(){
Scanner scan = new Scanner(System.in);
ArrayStack<String> stack = new ArrayStack<String>();
String s = \"\";
while(!s.equalsIgnoreCase(\"end\")){
System.out.print(\"Enter a word or end to \'quit\': \");
s = scan.next();
if(!s.equalsIgnoreCase(\"end\")){
stack.push(s);
}
}
System.out.println(\"Yo entered (in reverse): \");
while (!stack.isEmpty()) {
String word = stack.peek();
System.out.println(word);
stack.pop();
}
}
}
Output:
Enter a word or end to \'quit\': Hello
Enter a word or end to \'quit\': Java
Enter a word or end to \'quit\': World
Enter a word or end to \'quit\': end
Yo entered (in reverse):
World
Java
Hello
