#include using namespace std; const int MAX_LETTERS=20; // Capacity of the array. void display_reverse(char array[], int num_elems); // @param: array to reverse display // @param: num_elems is the number of elements in array. void reverse_array(char array[], int num_elems); // @param: array with num_elems in it. // @param: num_elems is the number of elements in the array. void fill_array(char array[], int size, int& num_elems); // @param array: the array to be filled // @param size: the capacity of the array parameter // @param num_elems: initialzed the number of elements in the array. // num_elems is updated with the additional elements. int main() { char array[MAX_LETTERS]; int num_letters = 0; // fill the array with letters, cout << "Enter up to " << MAX_LETTERS << " or enter '.' to end input:\n"; fill_array(array, MAX_LETTERS, num_letters); // Display the reverse, reverse it and display reverse again. display_reverse(array, num_letters); reverse_array(array, num_letters); display_reverse(array, num_letters); } void swap_values(char& a, char& b) // precondition: a and b are characters // postcondition: a and b are swapped. { char t = a; a = b; b = t; } void display_reverse(char array[], int num_elems) // precondition: an array of character values; // postcondition: output of the reverse of the array. { for (int i = num_elems-1; i >= 0; i--) cout << array[i]; cout << endl; } void reverse_array(char array[], int num_elems) // precondition: array with num_elems elements // postcondition: array reversed { for (int i = 0; i < (num_elems-1)/2; i++) swap_values(array[i], array[num_elems-i-1]); } void fill_array(char array[], int size, int& num_elems) // precondition: array with num_elems with capacity size. // postcondition: array with no more than capacity elems but // possibly more values added. num_elems is updated. { char next = ' '; // Holds next character. // Read values until user enters '.' or the array is full. while ( next != '.' && num_elems < size ) { cin >> next; if (next != '.') array[num_elems++] = next; } }