//Maxwell Brewer CS1A Chapter 10, p. 588, #2
//
/*******************************************************************************
* REVERSE ARRAY CHARACTERS
* _____________________________________________________________________________
* This program accepts a string input from the user (up to a maximum number
* of characters). It then reverses the order of characters in the string
* and displays the reversed result.
*
* INPUT:
* str : A character array entered by the user (maximum 50 characters).
*
* OUTPUT:
* The reversed version of the entered string.
*
* EXAMPLE:
* Input: Hello, World!
* Output: !dlroW ,olleH
*******************************************************************************/
#include <iostream>
#include <cstring> // For strlen function
using namespace std;
// Function prototype
void printReverse(char* str);
int main() {
char str[50]; // String to be entered by user
// User prompt and input
cout << "=============================================\n";
cout << "Please enter some text\n";
cout << "Note: with a max of 50 characters.\n";
cout << "Your string: ";
cin.getline(str, 50); // Gets a complete line
cout << "=============================================\n\n";
// Print reverse string
cout << "The reverse: ";
printReverse(str);
cout << "\n=============================================\n";
return 0;
}
// Function definition
void printReverse(char* str) {
// This function prints the string in reverse order.
// We start from the last character and print until the first character.
// NOTE: We get the position of the last character using strlen, which gives
// the number of characters in the string.
for (int i = strlen(str) - 1; i >= 0; i--) {
// Print characters in reverse
cout << str[i];
}
cout << endl; // End line for clean output
}