Solution:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <limits.h>
# define MAX 50
struct Stack
{
int top;
unsigned size;
char* array;
};
struct Stack* createStack(unsigned size)
{
struct Stack* stack = (struct Stack*) malloc(sizeof(struct Stack));
stack->size = size;
stack->top = -1;
stack->array = (char*) malloc(stack->size * sizeof(char));
return stack;
}
int isFull(struct Stack* stack)
{ return stack->top == stack->size - 1; }
int isEmpty(struct Stack* stack)
{ return stack->top == -1; }
void push(struct Stack* stack, char item)
{
if (isFull(stack))
return;
stack->array[++stack->top] = item;
}
char pop(struct Stack* stack)
{
if (isEmpty(stack))
return 0;
return stack->array[stack->top--];
}
void reverse(char str[])
{
int n = strlen(str);
struct 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()
{
int i,len;
char str[MAX] ;
printf("Enter the string\n");
scanf("%s",str);
len=strlen(str);
for(i=0; i<len; i++)
{
reverse(str);
}
printf("Reversed string is %s", str);
return 0;
}
0 Comments