Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

MUST BE WRITTEN IN C++ Write a C++ program that reads in a sequence of character

ID: 3572824 • Letter: M

Question

MUST BE WRITTEN IN C++

Write a C++ program that reads in a sequence of characters and prints them in reversed order. You must use a stack. Implement these two function prototypes.

1. void push(char *stack, int top, char c);

2. char pop(char *stack, int top);

Use provided driver program to test your code. You should get the same output.

***********************DRIVER PROGRAM********************

int main()

{

string str;

cout<<"Please input a string: "<<endl;

cin>>str;

char *stack;

stack =new char[str.length()];

int top=0;

  

for(int i=0;i<str.length();i++)

{

push(stack,top,str[i]);

  

}

for(int j=0;j<str.length();j++)

{

cout<<pop(stack,top);

top--;

}

}

input and output:

Please input a string:

abcdefg

gfedcba

Program ended with exit code: 0

Explanation / Answer

#include <iostream>
using namespace std;

void push(char *stack, int top, char c);
char pop(char *stack, int top);
int main()
{
string str;
cout<<"Please input a string: "<<endl;
cin>>str;
char *stack;
stack =new char[str.length()];
int top=0;
  
for(int i=0;i<str.length();i++)
{
push(stack,top,str[i]);
top++;
  
}

for(int j=0;j<str.length();j++)
{
cout<<pop(stack,top);
top--;
}
}

void push(char *stack,int top,char c)
{
*(stack+top)=c;
}
char pop(char *stack,int top)
{
return *(stack+top-1);
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote