Create an array of Strings, and using a stack, reverse the order of the Strings
ID: 3669229 • Letter: C
Question
Create an array of Strings, and using a stack, reverse the order of the Strings in the array. For example, the strings in the array might be Joe', Fred, "Mary". The reversed array would be "Mary Fred, Joe'. You should create a Stack class and a St class for this assignment, then use these classes in your software, Your stack should be a linked data structure, similar to a The main method should initialize an array of Strings-either with an explicit array declaration or by reading the data from afile. You should have at least five Strings in your array, but the software should work no matter how many Strings are in the array. Your stack class should maintain a stack pointer and the size of the stack, and should have methods to Push a new element onto a stack This method should put data onto the stack, which means it must first put the data in a stack element Pop an element off the stack and return the data in the element Print a list of the data in the stack. Return the current stack size.Explanation / Answer
import java.util.Stack;
public class StackArray {
public static void main(String[] args) {
Stack<String> st=new Stack<>();
String str[]={"hey" ,"this" ,"is" ,"amit" ,"mishra"};
System.out.println("The given string is");
for (int i = 0; i < str.length; i++) {
System.out.print(str[i]+" ");
}
for (int i = 0; i < str.length; i++) {
st.push(str[i]);
}
System.out.println(" The updates string/Reverse String is");
for (int i = 0; i < str.length; i++) {
System.out.print(st.pop()+" ");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.