Adsense 728 * 15

Search

Custom Search

Write a Program to Reverse a String Using Stack


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 first one to be removed.

Some important articles regarding Stack data structure and it's implementation.

Implement Stack data structure

Stack implementation using Linked List

MCQ on stack and queue for practice

Stack implementation using an Array

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

14 comments:

  1. hi can anyone please explain fibonacci search

    ReplyDelete
  2. for loop for push should have i<=len
    i.e for(i=0;i<=len;i++){
    push(str[i]);
    }

    ReplyDelete
  3. This comment has been removed by a blog administrator.

    ReplyDelete
  4. can i get the explaination of this codding??

    ReplyDelete
    Replies
    1. Stack 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.

      Delete
  5. In this program we are taking stack array as int data type.but it must be character data type.

    ReplyDelete
  6. In this program we are taking stack array as int data type.but it must be character data type.

    ReplyDelete
  7. In this program we are taking stack array as int data type.but it must be character data type.

    ReplyDelete
  8. In this we are taking the array stack as integer data type but it must be character datatype

    ReplyDelete
  9. In this we are taking array as integer data type but it must be character data type

    ReplyDelete