So given are two stack objects, which hold no value at first:
Stack stOne, stTwo;
Assuming that you have implemented ::size(), ::push(), ::pop()
, where pop returns the value stored on top of the stack, for your Stack objects, you want to push all chars of your given string into stOne
. Then you will have to pop stOne
for stOne.size()/2
amount of times and push the chars into stTwo
:
void solvePalindrome(std::string str){
for(int i=0; i<str.size(); i++){
stOne.push(str[i]);
}
for(int i=0; i<stOne.size()/2; i++){
stTwo.push(stOne.pop());
}
//do your comparison here
}
Alternatively, you can directly use str.size()/2 instead of stOne.size()/2 if you haven't implemented ::size()
yet.
Note: If you have an uneven amount of characters in your string, it will round down (floor) stOne.size()/2. All code is in CPP.