0

The objective is to have two strings, in which in this string, there is a backspace button represented as <. However, the output of two strings with differently positioned backspace button should be equal.

For the InputsEqual function, it basically pops the top item off the stack when it sees a backspace button.

I tested it out with a different file, but it still does not work. Can you please review this code?

#include <iostream>
#include <cstring>
#include <string>
#include <stack>
using namespace std;

bool EqualStacks(stack<char> a, stack<char> b);
bool InputsEqual(string inputA, string inputB);

int main()
{
    string inputA = "abcde<<";
    string inputB = "abcd<e<";

    if(InputsEqual(inputA,inputB))
    {
        cout << "Inputs Equal.\n";
    }
    else
    {
        cout << "Inputs are NOT Equal.\n";
    }
    return 0;
}

bool EqualStack(stack<char> a, stack<char> b)
{
    for(int i = 0; i < a.size(); i++)
    {
        if(a.top() == b.top())
        {
            a.pop();
            b.pop();
        }
    }
    return (a.empty() && b.empty()) ? true:false;
} 

//If both stacks are empty, they equal
bool InputsEqual(string inputA,string inputB) 
{
    stack<char> aStack;
    stack<char> bStack;
    // string inputA;
    // cout << "Please enter a string. Press < if you want to delete something"
    // cin >> inputA; 
    for(int i = 0 ; i < inputA.length() + 1; i++)
    {
        if(inputA[i] != '\0')
        {    
            if(inputA[i] != '<')
            {
                aStack.push(inputA[i]);
            }
            else if(!aStack.empty())
            {
                aStack.pop();
            }
            else
            {  
                aStack.pop();
            }         
        }
    }

    for(int i = 0 ; i < inputB.length() + 1; i++)
    {
        if(inputB[i] != '\0') 
        {
            if(inputB[i] != '<')
            {    
                bStack.push(inputA[i]);
            }
            else if(!bStack.empty())
            {
                bStack.pop();
            }
            else
            {  
                bStack.pop();
            }
        }
    }

    return (EqualStack(aStack,bStack));
} 

//Output: Strings are Not Equal

t.niese
  • 39,256
  • 9
  • 74
  • 101
TheRoadLessTaken
  • 531
  • 2
  • 7
  • 15
  • Welcome to Stack Overflow. Take a few minutes to read https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ for tips on how to debug your code. – Code-Apprentice Mar 03 '19 at 06:24
  • 1
    Unrelated (i think): `else { aStack.pop(); }`seems to be a bad idea. Only way to get there is if the stack is empty. Ergo nothing to pop. Boom. – user4581301 Mar 03 '19 at 06:29

2 Answers2

1

You have two specific issues breaking your code.

The first is that in the second copy of your loop in InputsEqual(), you push the value of inputA[i] where it should say inputB[i].

bStack.push(inputA[i]); // should be inputB!

Secondly, in EqualStack() you call a.size() on every iteration while comparing it to i. The issue is that you also shrink the stack by calling a.pop() when there's a match. This causes the for loop to abort early, leaving the stacks non-empty.

A quick fix is to change the loop to end when a is empty, like so:

for(int i = 0; !a.empty(); i++) // fixed

instead of:

for(int i = 0; i < a.size(); i++) // broken

I'd also like to point out a few other things that could be cleaned up real quick:

return (a.empty() && b.empty()) ? true:false; // redundant
return a.empty() && b.empty(); // better

and:

else if(!aStack.empty())
{
    aStack.pop(); // you call it if the stack is empty
}
else
{
    aStack.pop(); // and if its not, which is redundant!
}

pop() seems to be undefined for empty containers, from what I can tell, so checking if the stack is empty before calling it would be good practice, it's just that trailing pop() statement is unnecessary! Just wipe it out and you should be good.

Finally, you can avoid the inputAorB[i] != '\0' if you just check for inputAorB.length() instead of length() + 1 in the loop, so:

for(int i = 0 ; i < inputA.length() + 1; i++)
{
    if(inputA[i] != '\0')
    {
        if(inputA[i] != '<')
        {
            aStack.push(inputA[i]);
        }
        else if(!aStack.empty())
        {
            aStack.pop();
        }
    }
}

can be shortened to:

for(int i = 0 ; i < inputA.length(); i++)
{
    if(inputA[i] != '<')
    {
        aStack.push(inputA[i]);
    }
    else if(!aStack.empty())
    {
        aStack.pop();
    }
}

There is more cleanup possibly, but I figured I'd just point out the more glaring ones. Good luck with your project!

persona15
  • 200
  • 1
  • 8
0

You will never compare all elements of the stack the i < a.size() of your loop is evaluate each iteration, but you also change the size of a each iteration. So the comparisons with each iterations are:

  • 0<3
  • 1<2
  • 2<2

So it will stop after 2 comparision.

So at the end of the loop return (a.empty() && b.empty()) ? true:false; would return false because there are still elements in the stack.

Instead of your for loop you could just use a while loop:

while(!a.empty() && !b.empty()) {
  if(a.top() == b.top())
  {
    a.pop();
    b.pop();
  } else {
    break;
  }
}
t.niese
  • 39,256
  • 9
  • 74
  • 101