#include #include #include using namespace std; // PrintStack(vector stack) // Prints the contents of stack void PrintStack(vector stack) { // Loop through each character in the stack and print them preceded by their index for(int i = 0; i < stack.size(); i++) cout << i << ") " << stack[i] << endl; } // void ReverseStack(vector *stack) // Goes through each item in the stack and re-adds them in reverse void ReverseStack(vector *stack) { // Make a temporary copy of the stack, then clear the real one vector tmp = *stack; stack->clear(); // Loop through all items in the temporary stack in reverse, then add them to the real stack for(int i = 0; i < tmp.size(); i++) stack->push_back(tmp[(tmp.size() - 1) - i]); } int main() { // Create the stack vector stack; // Add an item to the stack string str = "racecar"; cout << "Analyzing string: " << str << endl; // Loop through the characters in str for(int i = 0; i < str.size(); i++) { // Strip out the character and add to stack char *ch = new char; sprintf(ch, str.substr(i, 1).c_str()); stack.push_back(*ch); } // Output the current stack cout << "Current stack is:\n"; PrintStack(stack); cout << endl; // Reverse the stack ReverseStack(&stack); // Print the new stack cout << "The stack is now:\n"; PrintStack(stack); // Wait for a key to be pressed before returning. getchar(); return 1; } OUTPUT: Analyzing string: racecar Current stack is: 0) r 1) a 2) c 3) e 4) c 5) a 6) r The stack is now: 0) r 1) a 2) c 3) e 4) c 5) a 6) r OUTPUT2: Analyzing string: RandomWord Current stack is: 0) R 1) a 2) n 3) d 4) o 5) m 6) W 7) o 8) r 9) d The stack is now: 0) d 1) r 2) o 3) W 4) m 5) o 6) d 7) n 8) a 9) R