//Zachary Abdollah CS1A Chapter 10, P. 588, #2
//
/*******************************************************************************
*
* DISPLAY BACKWARD STRING
*_______________________________________________________________________________
* This program accepts a C-string from the user and passes a
* pointer to the C-string to a function that displays its
* contents backward.
*_______________________________________________________________________________
* INPUT
* userString : C-string entered by the user
*
* OUTPUT
* None (the reversed C-string is displayed directly to the screen)
*
*******************************************************************************/
#include <iostream>
#include <cstring>
using namespace std;
// Function Prototype
void displayBackward(const char *strPtr);
int main ()
{
const int SIZE = 100; // Maximum C-string size
char userString[SIZE]; //INPUT
// Input String
cout << "Enter a string: ";
cin.getline(userString, SIZE);
// Output Result
cout << "The string backward is: ";
displayBackward(userString);
cout << endl;
return 0;
}
/*******************************************************************************
*
* FUNCTION displayBackward
*_______________________________________________________________________________
* This function accepts a pointer to a C-string and displays
* its characters in reverse order from the last character
* before the null terminator back to index 0.
*_______________________________________________________________________________
* INPUT
* strPtr : Pointer to a null-terminated C-string
*
* OUTPUT
* None (displays characters directly to console)
*
*******************************************************************************/
void displayBackward(const char *strPtr)
{
int length = strlen(strPtr); // Length of the C-string
for (int index = length - 1; index >= 0; index--)
{
cout << *(strPtr + index);
}
}