Travis D.
Travis D.

Reputation: 1

How can I modifiy my c++ program to show a word inputted by a user, backwards using a stack?

I want to assign a pointer to every character the user inputs. Then in doing so, I probably can use a loop to store the characters and a second loop rearrange the stack order using the pointers. But I don't know how to write that in a program form, and I'm not sure if it can work. Here is what I have so far:

#include<iostream>

using namespace std;

class Stack{
public:
  enum {MaxStack = 50};
  void init() {top = -1;}
  void push( char n ){
    if ( isFull() ) {
      cerr << "Full Stack. DON'T PUSH\n";
      return;
    }
    else {
      arr[ ++top ] = n;
      cout << "Just pushed " << n << endl;
      return;}
  }
  int pop() {
    if (isEmpty() ) {
      cerr << "\tEmpty Stack. Don't Pop\n\n";
      return 1;
    }
    else 
      return arr[top--];
  }
  bool isEmpty() {return top < 0 ? 1 : 0;}
  bool isFull() {return top >= MaxStack -1 ? top : 0;}
  void dump_stack() {
    cout << "The Stack contents, from top to bottom, from a stack dump are: " << endl;
    for (int i = top; i >= 0; i--)
      cout << "\t\t" << arr[i] << endl;
  }
private:  
  int top;
  int arr[MaxStack];
};

int main()
{   

    Stack a_stack;
    int x = 0;
    char inputchar;


    cout<<"Please enter a word"<<endl;
  a_stack.init();

while (inputchar != '.') //terminating char
 {
 cin >> inputchar;
 array[x] = inputchar;
 x++;
 }

int j = x;

for (int i = 0; i < j; i++)
 {
 cout << array[x];
 x--;
 }
  a_stack.push();

  a_stack.dump_stack();

return 0;
}  

Upvotes: 0

Views: 529

Answers (2)

Nikos C.
Nikos C.

Reputation: 51840

A stack, by its very LIFO nature (Last In, First Out), will reverse the order of anything you put in it. Example for string "Hello":

(The top of the stack is to the left)

H        push "H"
eH       push "e"
leH      push "l"
lleH     push "l"
olleH    push "o"

Now when you pop from the stack, you'll first get "o", then "l", etc. It's whatever you put in but in reverse order. You don't need to do anything special to achive that. Just push to stack in normal order, and when you pop you'll get it reversed:

// while loop
{
    cin >> inputchar;
    a_stack.push(inputchar);
}

// Display in reverse
while (not a_stack.isEmpty()) {
    cout << (char)a_stack.pop();
}

Here's a small example program using std::stack:
(No input error checking is done here.)

#include <iostream>
#include <stack>

int main()
{
    std::stack<char> st;
    char c = '\0';
    while (c != '.') {
        c = std::cin.get();
        st.push(c);
    }

    while (not st.empty()) {
        std::cout << st.top();
        st.pop();
    }
    std::cout << '\n';
}

Example input and output:

Hello world.
.dlrow olleH

Upvotes: 1

peterph
peterph

Reputation: 980

Unless using a stack is a must (i.e. it is a homework), you might be better off with getline(), its parameter delim (cf getline) followed by a reverse loop over the array. It would be faster, cleaner, less prone to errors and basically a two-liner.

Upvotes: 0

Related Questions