C++ Notes: Programming Problem - Words in Reverse Order

The problem

Write a program to do the following.

  1. Read "words" from cin.
  2. Save them in an array.
  3. Call a function, that you write, to reverse the order of the elements in the array. It would be easier to iterate in reverse over the array, but writing the function is part of this exercise.
  4. Write out the words in the array so the order is the opposite of the input order.

Use the string type

Use an array of strings (the library string type) to store the words. You can read "words" (non-blank characters) from the input like this.

// Fragment to read "words" and write them one per line.
string x;
while (cin >> x) {
    cout << x << endl;
}

You can adapt the above code to read into an array of strings.

The reverseArrayElements function

Write a function to reverse the elements of a string array. The prototype for the function should be:

void reverseArrayElements(string data[], int begin, int end);

where begin is the index of the first element in the range to be reversed, and end is the index after the last element in the range. Using an index to the last element beyond the range is common C++ practice.

One way to do this is to start with two indexes, one to the leftmost element (begin) and one to the rightmost element (end-1), swap the two values at these positions, increment the left index and decrement the right index. Continue this process as long as the left index is less than the right index.

Sample input/output

For the following input:

This is a test.

The output would be:

test.
a
is
This

Happy trails programming

You may ignore problems, eg, too much input.

A Solution

  1 
  2 
  3 
  4 
  5 
  6 
  7 
  8 
  9 
 10 
 11 
 12 
 13 
 14 
 15 
 16 
 17 
 18 
 19 
 20 
 21 
 22 
 23 
 24 
 25 
 26 
 27 
 28 
 29 
 30 
 31 
 32 
 33 
 34 
 35 
 36 
 37 
 38 
 39 
 40 
 41 
 42 
 43 
 44 
 45 
 46 
 47 
 48 
 49 
 50 
 51 
 52 
 53 
 54 
 55 
 56 
 57 
 58 
 59 
 60 
 61 
// arrayptr/reverse-word-order.cpp
// Fred Swartz - 2004-10-25

#include <string>
#include <iostream>
#include <cstdlib>    // for exit()
using namespace std;

//===================================================== prototype
void reverseArrayElements(string data[], int begin, int end);

//===================================================== constants
const int MAX_SIZE = 1000;

//========================================================== main
int main() {
    //... Local variables
    string words[MAX_SIZE];  // Place to save all words
    int    n;                // Number of words
    
    //... Read words into an array,
    n = 0;
    while (cin >> words[n]) {
        n++;
        if (n >= MAX_SIZE) {
            cerr << "reverse-word-order: Too much data" << endl;
            exit(1);   // Terminate with code indicating errror.
        }
    }
    
    //... Reverse order of the array elements.
    reverseArrayElements(words, 0, n);
    
    //... Print the array
    for (int i=0; i<n; i++) {
        cout << words[i] << endl;
    }
    
    system("PAUSE"); // NON-PORTABLE to keep Dev-C++ window open.
    return 0;
}

//============================================== reverseArrayElements
// Reverse the order of elements in an array of strings.
//   data - array of strings.
//   begin - index of first element in range to be reversed.
//   end   - index of first element beyond end of range.
void reverseArrayElements(string data[], int begin, int end) {
    int left  = begin;
    int right = end-1;
    
    while (left < right) {
        string temp;
        temp        = data[left]; 
        data[left]  = data[right]; 
        data[right] = temp;
        
        left++;
        right--;
    }
}