Question

In: Computer Science

how can I save a character stack to a string and then save that string into...

how can I save a character stack to a string and then save that string into a new string arraylist in java?

so if the character stack is h, e, l, l, o
i want it to save "hello" to a string and then put that string into an array list.

Solutions

Expert Solution

import java.io.*;

class String_Stack
{
   int N, top=-1;
   char A[];
   String_Stack(int x)
   {
       N=x;
       A= new char [N];
   }

   void Push(char n)
   {
       if(top==N-1)
           System.out.println("Stack Overflow");
       else
       A[++top]= n;
   }

   char Pop()
   { char m;
       if(top==-1)
           System.out.println("Stack Underflow");
       else
       {   m= A[top]; --top; }
       return m;
   }
      
   void display()
   {
       if(top==-1)
           System.out.println("Stack underflow");
       else {
      
       for(int i=0; i<=top; i++)
           System.out.println(A[i]); }
   }
  
   public static void main(String args[])throws IOException
   {
       BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
      
       System.out.println("Enter the length of the stack: ");
       int n= Integer.parseInt(br.readLine());
       String_Stack obj= new String_Stack(n);
      
       do{
       System.out.println("1. Push character.");
       System.out.println("2. Pop character.");
       System.out.println("3. Display Stack.");
      
       System.out.println("Enter your choice: ");
       int ch= Integer.parseInt(br.readLine());

       switch(ch)
       {
           case 1:
               System.out.println("Enter the character");
               char c= br.readLine();

               obj.Push(c);
               break;
           case 2:
  
               char m=obj.Pop();
               System.out.println("The character popped is: "+m);
               break;
          
           case 3:
               obj.display();
               break;
           default:
               System.out.println("Invalid Entry");
       }
       System.out.println("Do you wish to continue(Y/N)?");
       char ch2= br.readLine();
       }
       while(ch2=='Y' || ch2=='y');
       //exits from while considering that the final string has been stored

       String S="";
      
       for(int i=top; i>=0; i--)//since the word is stored in reverse order in the stack
       {
           S= S+A[i];
       }

       ArrayList al=new ArrayList();
             al.add(S);
   }
}


Related Solutions

How can I check that if a string only make from specific character? For example, I...
How can I check that if a string only make from specific character? For example, I want to check if a string only make from ICDM characters. It pass the test if it makes from ICMD. It fail the test if it makes from any special character like @#$& or lower case. Example inputs: if string = CIM, it passed if string = AIM, it failed if string = MCDI, it passed if string = ICDF, it failed This can...
To begin, write a program to loop through a string character by character. If the character...
To begin, write a program to loop through a string character by character. If the character is a letter, print a question mark, otherwise print the character. Use the code below for the message string. This will be the first string that you will decode. Use the String class method .charAt(index) and the Character class method .isLetter(char). (You can cut and paste this line into your program.) String msg = "FIG PKWC OIE GJJCDVKLC MCVDFJEHIY BIDRHYO.\n"; String decryptKey = "QWERTYUIOPASDFGHJKLZXCVBNM";...
the purpose of the code is to implement a stack how can i convert this code...
the purpose of the code is to implement a stack how can i convert this code in a way that these changes apply to it? // simplify the logic of the main // getline(cin, line) needs to be changed to a normal cin >> line; //make a function for 'list' // not allowed temporary stack (stack s2) //merge the catches (errors) // introduce another function for main // avoid repetitive code here is the code: #include #include #include #include #include...
Can someone please tell me on how can I have a double and character as one...
Can someone please tell me on how can I have a double and character as one of the items on my list? Thanks! This is what I got so far and the values I am getting are all integers. #pragma once #include <iostream> class Node { public:    int data;    Node* next;       // self-referential    Node()    {        data = 0;        next = nullptr;    }    Node(int value)    {        data...
In the Stack Module I gave you a project that shows how to create a Stack...
In the Stack Module I gave you a project that shows how to create a Stack using doubly linked nodes. StackWithDoublyLinkedNodes.zip It is a template class that needs the Node class. You must use this same Node class (no alterations) to create a Queue class . public class Queue <T>{ } Use the NetBeans project above as an example. I have put a driver program (q1.java) in the module. Also here:  q1.java This driver program should then run with your Queue...
In the Stack Module I gave you a project that shows how to create a Stack...
In the Stack Module I gave you a project that shows how to create a Stack using doubly linked nodes. StackWithDoublyLinkedNodes.zip It is a template class that needs the Node class. You must use this same Node class (no alterations) to create a Queue class . public class Queue <T>{ } Use the NetBeans project above as an example. I have put a driver program (q1.java) in the module. Also here:  q1.java This driver program should then run with your Queue...
class ArrayStringStack { String stack[]; int top; public arrayStringStack(int size) { stack = new String[size]; top...
class ArrayStringStack { String stack[]; int top; public arrayStringStack(int size) { stack = new String[size]; top = -1; } //Assume that your answers to 3.1-3.3 will be inserted here, ex: // full() would be here //empty() would be here... //push() //pop() //displayStack() } 1. Write two boolean methods, full( ) and empty( ). 2. Write two methods, push( ) and pop( ). Note: parameters may be expected for push and/or pop. 3. Write the method displayStack( ) that prints the...
Write a C program that will read a character string and then encrypt the string based...
Write a C program that will read a character string and then encrypt the string based on one of the 3 different encryption methods. The type of encryption is to be selected by the user. Encryption method 1: Swapping by position. Characters in the array are swapped with the opposite characters based on their position in the string. Example: Input string – apple. Encrypted string – elppa Method: The first character ‘a’ and the last character ‘e’ – swap their...
JAVA I need to make it that I can expand the stack for everytime a push...
JAVA I need to make it that I can expand the stack for everytime a push operation is performed that would normally cause an overflow, returning a false value. Should accompany 3 nodes initially. DriverStack.java public class DriverStack {    public static void main (String[] args) {    Stack s = new Stack(3); Listing l; Listing l1 = new Listing ("Bill", "1st Avenue", "123 4567"); Listing l2 = new Listing ("Alec", "2nd Avenue", "456 4567"); Listing l3 = new Listing...
HOW CAN I USE a string instead of array tries and res on this assignment, with...
HOW CAN I USE a string instead of array tries and res on this assignment, with out impacting the program or modifying too much on conditions  check code bellow import java.util.Scanner; import java.util.Random;//starter code provided public class inputLap { public static char roller; public static String playerName; public static int printed=0; public static int rounds=8,lives=0,randy; public static int tries[]=new int[4];//use arrays to store number of tries in each life public static int res[]=new int[4]; public static String getName(String aString){ Scanner sc=...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT