Hi,
I want to use 3 stacks to check a word that user entered whether is it Palindrome or not.
I have written this code:
But I don't know how to use 3 stacks in "main".
I appreciate your help.
I don't want to use back and front.
Why exactly are you using a stack to check if a string is a palindrome? All you have to do is have one iterator from the beginning and the other from the back and if they do not equal they are not palindromes.
1 2 3 4 5 6 7
bool palindrome = true;
for(auto it = str.begin(), it2 = str.end()-1; it < it2 && palindrome; ++it , --it2)
//if the string is odd characters you can iterate while they are not equal
//but for evens they will not equal each other
{
palindrome = *it == *it2;
}
The question remains the same, why 3? Why not just 2? If you plan to use 3, then explain your reason or how you want to do it.
It can be done with 2 by having iterators (pointers) at the front and back of the string. Then as you move the iterators towards each other, you simply push the character at each iterator into the respective stack. In the end you start popping from each stack and comparing the values you get; if any of the values are not the same, then the string is not a palindrome