Reverse String using Stack
Write a C Program To Reverse String using Stack. Here’s simple Program To Reverse String using Stack in C Programming Language.
What is Stack ?
Stack is an abstract data type with a bounded(predefined) capacity. It is a simple data structure that allows adding and removing elements in a particular order. Every time an element is added, it goes on the top of the stack, the only element that can be removed is the element that was at the top of the stack, just like a pile of objects.
Basic Operations : :
- push() − Pushing (storing) an element on the stack.
- pop() − Removing (accessing) an element from the stack.
- peek() − get the top data element of the stack, without removing it.
- isFull() − check if stack is full.
- isEmpty() − check if stack is empty.
Below is the source code for C Program To Reverse String using Stack which is successfully compiled and run on Windows System to produce desired output as shown below :
SOURCE CODE : :
/* C Program To Reverse String using Stack */ #include<stdio.h> #include<string.h> #include<stdlib.h> #define MAX 20 int top = -1; char stack[MAX]; char pop(); void push(char); int main() { char str[20]; unsigned int i; printf("Enter the string : " ); gets(str); /*Push characters of the string str on the stack */ for(i=0;i<strlen(str);i++) push(str[i]); /*Pop characters from the stack and store in string str */ for(i=0;i<strlen(str);i++) str[i]=pop(); printf("\nReversed string is : "); puts(str); return 0; }/*End of main()*/ void push(char item) { if(top == (MAX-1)) { printf("\nStack Overflow\n"); return; } stack[++top] =item; }/*End of push()*/ char pop() { if(top == -1) { printf("\nStack Underflow\n"); exit(1); } return stack[top--]; }/*End of pop()*/
OUTPUT : :
/* C Program To Reverse String using Stack */ Enter the string : CodezClub Reversed string is : bulCzedoC
If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach up to you in short interval.
Thanks for reading the post….