All pastes #927083 Raw Edit

Anonymous

public cpp v1 · immutable
·published 2008-03-04 00:49 UTC
rendered paste body
#include <string>#include <iostream>#include <vector>using namespace std;// PrintStack(vector <char> stack)//   Prints the contents of stackvoid PrintStack(vector <char> 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 <char> *stack)//   Goes through each item in the stack and re-adds them in reversevoid ReverseStack(vector <char> *stack){	// Make a temporary copy of the stack, then clear the real one	vector <char> 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 <char> 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;}