//Cameron Pham CS1A Chapter 10, p. 588, #2
//
/******************************************************************************
* DISPLAY CONTENTS BACKWARD
* ______________________________________________________________________________
* This program accepts a pointer to a C-string and displays its characters in
* reverse order.
* ______________________________________________________________________________
* INPUT
* String // Characters inputted
*
* OUTPUT
* Reverse // Characters put in reverse
*******************************************************************************/
#include <iostream>
using namespace std;
// Function prototype
void displayBackward(char* str);
int main()
{
const int SIZE = 100;
char userString[SIZE];
// Get a string from the user
cout << "Enter a string: ";
cin.getline(userString, SIZE);
// Display the string backward
cout << "String backward: ";
displayBackward(userString);
cout << endl;
return 0;
}
// Display the characters of a C-string backward
void displayBackward(char* str)
{
int length = 0;
// Determine the length of the string
while (*(str + length) != '\0')
{
length++;
}
// Display the characters in reverse order
for (int count = length - 1; count >= 0; count--)
{
cout << *(str + count);
}
}