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

Reverse a string using stack in c++ im struggling a little bit with probably the

ID: 3695903 • Letter: R

Question

Reverse a string using stack in c++

im struggling a little bit with probably the more simple parts of this program. I was given a very specific example and structure in class using integers and how to check if you can push or pop without error. Before setting up the stack we gave it a specific MAX_SIZE of 5, so if it was bigger than 5 it would return that it wasnt possible to push. That brings my first problem. Since you don't know the length of the string the user will enter to be reversed can the MAX_SIZE be a variable like l for length that changes upon input? And if thats the case, then would I need to still have the error protecting you from pushing a full stack? An example would be great.

If my question doesn't make sense, im basically just wondering how to reverse a user input string using stacks in c++

Thank you!

Explanation / Answer

you can do this in two way. 1) first way is you can specify the required string to reverse in the program while writing the code (this is static allocation of memory). so you can set MAX_size according to the length of the string you given. the example code is given below,

source code:

#include<iostream.h>
#include<conio.h>
#include<string.h>

#define max 100
int top;
char stack[max];
class reverse
{
public:
void push(char);
void pop();

};

void reverse:: push(char x)
{
if(top==max-1)
{
cout<<"stack overflow ";
}
else
{
stack[++top]=x;

}
}

void reverse:: pop()
{
cout<<stack[top--];
}
void main()
{
reverse obj;
char str[]="this is an example string";
int ln,i;
clrscr();
ln=strlen(str);
for(i=0;i<ln;i++)
obj.push(str[i]);
for(i=0;i<ln;i++)
obj.pop();
getch();
return;
}

2) second method is dynamically allocating memory for stack. here stack size is determining after user entered the input. code is given below,

source code:

#include <iostream.h>

#include<stdio.h>
#include <string.h>
#include <stdlib.h>
#include <limits.h>
#include<conio.h>


struct Stack
{
int top;
unsigned capacity;
char* str;
};

// here memory allocating dynamically according to the user input

struct Stack* createStack(unsigned capacity)
{
struct Stack* stack = (struct Stack*) malloc(sizeof(struct Stack));
stack->capacity = capacity;
stack->top = -1;
stack->str = (char*) malloc(stack->capacity * sizeof(char));
return stack;
}

void push(struct Stack* stack, char item)

{

if(stack->top == stack->capacity - 1)
cout<<" stack reached to the last index ";
else
stack->str[++stack->top] = item;
}

char pop(struct Stack* stack)
{
if (stack->top == -1)
   {
   cout<<"stack is empty ";
   return INT_MIN;
   }
   else
return stack->str[stack->top--];
}
void reverse(char str2[100])
{

int n = strlen(str2);

//length of the user input string is passing to the creatstack function to allocate memory
struct Stack* stack = createStack(n);
int i;
for (i = 0; i < n; i++)
   push(stack, str2[i]);

for (i = 0; i < n; i++)
   str2[i] = pop(stack);
}

int main()
{
char str2[100];
clrscr();
cout<<"enter a string : ";
gets(str2);
reverse(str2);
cout<<"Reversed string is : " <<str2;
getch();
return 0;
}

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