//Ryan Shahriyarpour   CS1A  Chapter 10   Question 2
/*****************************************************************************
 *
 * Backward String
 *
 *********************
 *
 * This program asks the user to enter a string, then passes a pointer to
 * that C-string to a function that displays the contents backward.
 * For example, "Gravity" is displayed as "ytivarG".
 *
 * INPUT
 *  userString - a line of text entered by the user
 *
 * OUTPUT
 *  The same text displayed in reverse order
 *
 ****************************************************************************/

#include <iostream>
#include <cstring>
using namespace std;

// Function prototype
void displayBackward(const char *str, int length);

int main()
{
    // DATA DICTIONARY
    // SIZE       - size of the character array (constant)
    // userString - the C-string entered by the user
    // length     - number of characters in the string
    const int SIZE = 100;
    char userString[SIZE];
    int length;

    // Get the string from the user
    cout << "Enter a string: ";
    cin.getline(userString, SIZE);
    length = strlen(userString);

    // Display the string backward
    cout << "Backward: ";
    displayBackward(userString, length);
    cout << endl;

    return 0;
}

// Displays a C-string backward. The length parameter is used as the
// counter, starting at the end of the string and counting down to the
// first character.
void displayBackward(const char *str, int length)
{
    while (length > 0)
    {
        length--;
        cout << *(str + length);
    }
}