#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; }