Question

C++ Using the Stack operations, write a pseudocode routine, dupA, that takes aStack for string, checks...

C++
Using the Stack operations, write a pseudocode routine, dupA, that takes aStack for string, checks to see if the top starts with ‘A’ or ‘a’. If so, duplicate the top of the stack (i.e. push a copy of that value onto the stack) else if length > 10, pop it off the stack

0 0
Add a comment Improve this question Transcribed image text
Answer #1

C++ implentation of above problem is in following screenshot:

1 #include <iostream> 2 #include <stack> 3 using namespace std; 4 5 | stack<char> st; // Global stack variable 6 7 / void dis

로 27 28 29 30 31 32 33 34 35 36 else if (st.size() >10) // condition checking for stack length { cout<<Stack length is: <<s

Here the output:

if TOS(top of stack) = a

Enter any String: vishwasa TOP of Stack: a Duplicating the TOP of Stack... Stack elements are: a a a W h S i Process returned

if TOS= A

Enter any String: vishwash TOP of Stack: A Duplicating the TOP of Stack... + Stack elements are: A A Н S a W h IS i V Process

if stack length > 10

Enter any String: vishwasdfgh Stack length is: 11 Stack elements are: h 9 f d d S a W h S i Process returned 0 (oxo) executio

if no condition is matched

Enter any String: vishu Stack elements are: น h S i Process returned 0 (oxo) execution time : 3.167 s Press ENTER to continue

Here is the code:

#include <iostream>
#include <stack>
using namespace std;

stack<char> st;   // Global stack variable

void display()    // Stack display function
{   while (!st.empty()) {
    cout<< st.top()<<"\n";
    st.pop();
   }
}
int main() {
   string name;     // user input string
   cout<<"Enter any String: ";
   cin>>name;
   for(int i=0;i<name.size();i++) // pushing the elements into stack
     st.push(name[i]);
   if(st.top()=='A' || st.top()=='a') // condition checking for A or a
    {
        cout<<"TOP of Stack: "<<st.top()<<"\n";
        st.push(st.top());
        cout<<"Duplicating the TOP of Stack....\n";
        cout<<"Stack elements are: \n";
        display();
    }
    else if (st.size()>10) // condition checking for stack length
    {
        cout<<"Stack length is: "<<st.size()<<"\n";
        cout<<"Stack elements are: \n";
        display();
    }
    else {cout<<"Stack elements are: \n";
        display();}
}

Add a comment
Know the answer?
Add Answer to:
C++ Using the Stack operations, write a pseudocode routine, dupA, that takes aStack for string, checks...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT