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