0% found this document useful (0 votes)
32 views5 pages

Ans-#Include : 1. Write A C++ Program To Reverse Your Full Name Using Stack

The document contains code to reverse a name using a stack and code to check for balanced parentheses in an expression using a stack. The name reversal code defines a Stack class and functions to create a stack, check if it is full/empty, and push/pop elements. It uses these functions to push the name onto the stack and then pop it off in reverse order. The balanced parentheses code uses a stack to push opening parentheses and pop them, checking that the closing parentheses match the type on top of the stack.

Uploaded by

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

Ans-#Include : 1. Write A C++ Program To Reverse Your Full Name Using Stack

The document contains code to reverse a name using a stack and code to check for balanced parentheses in an expression using a stack. The name reversal code defines a Stack class and functions to create a stack, check if it is full/empty, and push/pop elements. It uses these functions to push the name onto the stack and then pop it off in reverse order. The balanced parentheses code uses a stack to push opening parentheses and pop them, checking that the closing parentheses match the type on top of the stack.

Uploaded by

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

1. Write a c++ program to reverse your full name using stack.

Ans- #include <iostream>


#include<string.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[] = "MAYANK RAJ";

reverse(str);
cout << "Reversed string is " << str;

return 0;
}

OUTPUT-

2. Write a c++ program for checking baalanced paranthesis in an


expression.

Ans- #include <iostream>

#include <stack>
using namespace std;

bool isBalancedExp(string exp) {

stack<char> stk;

char x;

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

if (exp[i]=='('||exp[i]=='['||exp[i]=='{') {

stk.push(exp[i]);

continue;

if (stk.empty())

return false;

switch (exp[i]) {

case ')':

x = stk.top();

stk.pop();

if (x=='{' || x=='[')

return false;

break;

case '}':

x = stk.top();

stk.pop();

if (x=='(' || x=='[')

return false;

break;

case ']':

x = stk.top();

stk.pop();
if (x =='(' || x == '{')

return false;

break;

return (stk.empty());

int main() {

string expresion = "()[(){()}]";

if (isBalancedExp(expresion))

cout << "This is Balanced Expression";

else

cout << "This is Not Balanced Expression";

OUTPUT-

You might also like