// Elaine Torrez CS1A
// ____________________________________________________________
//
// DISPLAY STRING BACKWARD
// ____________________________________________________________
// This program prompts the user to enter a string and then
// displays the string in reverse order.
// ____________________________________________________________
// INPUT
// userStr : The string entered by the user
//
// OUTPUT
// backward : The string displayed backward
// ____________________________________________________________
#include <iostream>
using namespace std;
// FUNCTION PROTOTYPE
void displayBackward(const char *str);
int main()
{
/****************************************************
* VARIABLE DECLARATIONS
****************************************************/
const int SIZE = 100;
char userStr[SIZE]; // INPUT string
/****************************************************
* INPUT
****************************************************/
cout << "Enter a string: ";
cin.getline(userStr, SIZE);
/****************************************************
* OUTPUT
****************************************************/
cout << "\nString backward: ";
displayBackward(userStr);
cout << endl;
return 0;
}
// *********************************************************
// displayBackward
// ---------------------------------------------------------
// Displays all characters of a C-string in reverse order.
// *********************************************************
void displayBackward(const char *str)
{
int length = 0;
// Find the length of the string manually
while (str[length] != '\0')
length++;
// Print characters from back to front
for (int i = length - 1; i >= 0; i--)
cout << str[i];
}