string using a stack, reverse a string, stack implementation
Write a program in C language to reverse a string using a stack.

Brief overview of stack implementation is given in the below picture:





Implementation of above problem using a stack.
#include<stdio.h> 
#include<string.h> 
#include<stdlib.h> 
#define MAX 20 
int top = -1; 
char stack[MAX]; 
char pop(); 
void push(char); 
main() 
char str[20]; 
unsigned int i; 
printf("Enter the string : " ); 
gets(str); 
for(i=0;i<strlen(str);i++) 
push(str[i]); 
for(i=0;i<strlen(str);i++) 
str[i]=pop(); 
printf("Reversed string is : "); 
puts(str); 
void push(char item) 
if(top == (MAX-1)) 
printf("Stack Overflow\n"); 
return; 
stack[++top] =item; 
}
char pop() 
if(top == -1) 
printf("Stack Underflow\n"); 
exit(1); 
return stack[top--]; 
}


Thanks
Mukesh Rajput
Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments:

Thanks
Mukesh Rajput