0% found this document useful (0 votes)
2 views1 page

Stringreverse Program

The document contains C++ code that implements a stack data structure to reverse a string. It defines a Stack class with methods for creating a stack, checking if it is full or empty, pushing and popping elements, and reversing a string using the stack. The main function demonstrates the reversal of the string 'friends', resulting in 'sdneirf'.

Uploaded by

lingalsrinu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views1 page

Stringreverse Program

The document contains C++ code that implements a stack data structure to reverse a string. It defines a Stack class with methods for creating a stack, checking if it is full or empty, pushing and popping elements, and reversing a string using the stack. The main function demonstrates the reversal of the string 'friends', resulting in 'sdneirf'.

Uploaded by

lingalsrinu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

#include<bits/stdc++.

h>
using namespace std;
class Stack
{
public:
int top;
unsigned capacity;
char* array;
};
Stack* createStack(unsigned capacity)
{
Stack* stack=new Stack();
stack->capacity=capacity;
stack->top=-1;
stack ->array=new char [(stack->capacity * sizeof(char))];
return stack;
}
int isFull(Stack* stack)
{
return stack->top==stack->capacity-1;
}
int isEmpty(Stack* stack)
{
return stack->top==-1;
}
void push(Stack* stack, char item)
{
if(isFull(stack))
return;
stack->array[++stack->top]=item;
}
char pop(Stack* stack)
{
if(isEmpty(stack))
return -1;
return stack->array[stack->top--];

}
void reverse(char str[])
{
int n = strlen(str);
Stack* stack=createStack(n);
int i;
for(i=0;i<n;i++)
push(stack,str[i]);
for(i=0; i<n; i++)
str[i]=pop(stack);
}
int main()
{
char str[]="friends";
reverse(str);
cout<<"Reversed String is……. "<<str;
return 0;
} Output: Reversed String is……. sdneirf

You might also like