I'm trying to write a program using a stack to determine if the two sequences of characters are the mirror image of each other. Can anyone help me to get started?

Recommended Answers

All 2 Replies

Consider the definition of a stack - last-in, first-out. If you load a bunch of stuff into a stack, and then take it all out again, you now have your original items *in reverse order*.

Consider the definition of a stack - last-in, first-out. If you load a bunch of stuff into a stack, and then take it all out again, you now have your original items in reverse order.[/QUOTE]

Thank you so much for your response. I understand the logic but somehow it's not reading that it's the reverse when I enter the two strings like this for example....hello:olleh. It should say "These two strings ARE the reverse of each other but instead it's saying that they are not. Can you please look over this code and see what I've done wrong. I know it's something with my code in main. If you could please reply as soon as possible I would really appreciate your help. Again Thanks.........

#include<iostream>

using namespace std;

const char MAX_ITEMS = 50;


class Stack
{
    char mElements[MAX_ITEMS];
    char mLetter;
    int mTop; 

public:
    Stack();   
    bool IsEmpty()const; 
    bool IsFull(); 
    bool Push(char letter); 
    bool Pop(char &letter); 
    void MakeEmpty(); 
};


int main()
{

    Stack myStack;
    char letter;
    char other_letter;
    char ans;
    bool different = false;

    do
    {
        cout<< "Enter two strings separated only by a : ";
        cin.get(letter);
        while(letter!= ':' && letter!= '\n' && !myStack.IsFull())
        {
            myStack.Push(letter);
            cin.get(letter);
        }
        if (letter != ':') 
        cin.get(letter);
        different = true;

        while (!myStack.IsEmpty() && letter!= '\n')
        {   
            myStack.Pop(other_letter);
            if (other_letter != letter)
            different = true;
            cin>>letter;
        }

        if (!myStack.IsEmpty() && letter =='\n')
            different = true;

        if(myStack.IsEmpty() && letter != '\n')
            {
            while (letter!= '\n')
            cin.get(letter);
            different = true;
            }

        if (!different )
        {
            cout<< "The two strings ARE the reverse of     each other. ";
            cout<<endl;
        }
        else 
        {
            cout<< "The two strings ARE NOT the reverse of each other. ";
            cout<<endl;
        }
        cout<< "Do you want to enter another set?(y/n) ";
        cin>>ans;
        cin.get(letter);

        myStack.MakeEmpty();

    }
        while (ans!= 'n' && ans!= 'N');
        return 0;
    }


Stack::Stack()
{
    mTop = -1;
}

void Stack::MakeEmpty()
{
    mTop = -1;
}

bool Stack::IsEmpty()const
{
    return(mTop == -1);
}

bool Stack::IsFull()
{
    return(mTop == MAX_ITEMS -1);
}

bool Stack::Push(char letter)
{
    if(!IsFull())
    {
        mTop++;
        mElements[mTop] = letter;
        return true;
    }
    else
        return false;
}

bool Stack::Pop(char &letter)
{
    if(!IsEmpty())
    {
        mLetter=mElements[mTop];
        mTop--;
        return true;
    }
    else
        return false;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.