//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;
// Constant for the size of the character array
const int SIZE = 100;
// Function prototype
void displayBackward(const char *str);
int main()
{
// DATA DICTIONARY
// SIZE - size of the character array (constant)
// userString - the C-string entered by the user
char userString[SIZE];
// Get the string from the user
cout << "Enter a string: ";
cin.getline(userString, SIZE);
// Display the string backward
cout << "Backward: ";
displayBackward(userString);
cout << endl;
return 0;
}
// Displays the contents of a C-string backward.
// Starts at the last character and walks back to the first.
void displayBackward(const char *str)
{
// DATA DICTIONARY
// index - position of the character being displayed
int index;
for (index = strlen(str) - 1; index >= 0; index--)
cout << *(str + index);
}