Please send questions to
st10@humboldt.edu .
//---------------------------------------------------------------------
// File: reverseString.cpp
// Name: Sharon M.Tuttle
// last modified: 3-21-04
//
// Contract: reverseString : string -> string
//
// Purpose: returns a string whose characters are those of myString,
// reversed.
//
// preconditions: none
//
// postconditions: string returned has the characters of myString
// in reverse order
//
// Examples: reverseString("george") == "egroeg"
// reverseString("Madam, I'm Adam") == "madA m'I ,madaM"
// reverseString("") == ""
// reverseString("A") == "A"
//
// libraries/other functions used: string, stack.h
//---------------------------------------------------------------------
#include <string>
#include "stack.h"
using namespace std;
string reverseString (string myString)
{
stack<char> charStack;
string reversedStr;
char nextChar;
// push the characters of myString onto the stack
for (int i=0; i < myString.length(); i++)
{
charStack.push(myString[i]);
}
// pop the characters off the stack to get the
// reversed string
while (! charStack.is_empty( ))
{
nextChar = charStack.get_top( );
charStack.pop( );
reversedStr += nextChar;
}
// reversedStr now has the desired reversed string
return reversedStr;
}