Write a C, C++ program to reverse a string using Stack data structure. In this question, A string is input by a user and our program will reverse a string using a Stack.
Stack Data Structure
Stack is an abstract data type in which push (insertion) and pop (deletion) operations can be done at one end. Stack is a LIFO (Last in First Out) data structure, which means element which is inserted at last must be the first one to be removed.
Some important articles regarding Stack data structure and it's implementation.
Stack program in C using an Array
Implement Stack data structure
Stack implementation using Linked List
MCQ on stack and queue for practice
Reverse a string using for and while loop
Program to Reverse a String Using Stack Data Structure
#include <stdio.h> #include <string.h> #define max 100 int top,stack[max]; void push(char x){ // Push(Inserting Element in stack) operation if(top==max-1){ printf("stack overflow"); } else { stack[++top]=x; } } void pop(){ // Pop (Removing element from stack) printf("%c",stack[top--]); } main() { char str[]="I Love Programming"; int len = strlen(str), i; for(i=0; i<len; i++) push(str[i]); for(i=0; i<len; i++) pop(); }
Output
gnimmargorP evoL I
Programming questions on String
C++ Program to Reverse a String using Stack
#include <iostream> #include <string.h> #include <stack> // Standard library for creating stack using namespace std; void reverse(char *str, int len) { stack<char> s; int i; // Push into a stack for(i = 0; i < len; i++) s.push(str[i]); // Pop from a stack for(i= 0; i < len; i++) { str[i] = s.top(); s.pop(); } } int main() { char str[]="I Love Programming"; int len = strlen(str); reverse(str, len); cout <<"After reversing a string \n" << str; return 0; }
C, C++ interview questions
WRONG OUTPUT
ReplyDeleteHi Pooja, what's wrong in it.
DeleteI think the output is correct.
DeleteI have output as enter the string by user and then reverse that string
Deletehi can anyone please explain fibonacci search
ReplyDeletehttp://www.cprogrammingcode.com/2014/06/program-to-print-fibonacci-series-using.html
Deletefor loop for push should have i<=len
ReplyDeletei.e for(i=0;i<=len;i++){
push(str[i]);
}
Array a[n] consists of elements from a[0] to a[n-1].So no need of taking '=' sign in the 'for' loop.
DeleteThis comment has been removed by a blog administrator.
ReplyDeletecan i get the explaination of this codding??
ReplyDeleteStack is a data structure in which an element that pushed last can be the first element to be popped. So, In this program we pushed all character of a string a stack and then we popped it.
DeleteIn this program we are taking stack array as int data type.but it must be character data type.
ReplyDeleteIn this program we are taking stack array as int data type.but it must be character data type.
ReplyDeleteIn this program we are taking stack array as int data type.but it must be character data type.
ReplyDeleteIn this we are taking the array stack as integer data type but it must be character datatype
ReplyDeleteThis comment has been removed by a blog administrator.
ReplyDeletecan anyone explain what value will be assign to top variable initially??
ReplyDeleteFor Stack data structure, by default top variable always point to last pushed value in stack .
Deletecan anyone pls explain what value should assign for top variable initially?
ReplyDelete