The reverse program shown in the slides of chapter 13-1 (also present in the book) reads from a file/command line using system calls and outputs the lines in reverse order on the terminal. The last line is swapped with the first line , the second last is swaped with the second line and so on. It also has a -c option to run with. When used with -c option it reverse the characters of each line as well. For example,

Lets say below are the file contents of your file.

This is line 1

This is line 2

This is line 3

This is line 4

This is line 5

When executed without the -c option the output on terminal would be as follows:

This is line 5

This is line 4

This is line 3

This is line 2

This is line 1

(The order of lines are reversed, fifth line then fourth line then third and so on.) When you run the program with -c option the output would be as follows

After reversing the order of lines, the characters of each line are also reversed. Your homework should you choose to accept it (Of course you have too :P) is to implement this and also implement a -r option. Everything would work the same until the user uses -r option instead of -c. When -c option is used then you would not swap each character of a line rather you would reverse each word. So when used the -r the output would be as follows:

The words are swapped/reversed in each line (last word then second last then third last and so on).

You can assume the following things:

The user will only use file option.

The file will only contain letters, numbers and spaces.

THIS IS THE CODE

#include<iostream>
#include<string>
#include<sstream>
#include<fstream>
#include<string.h>

using namespace std;

int main(int argc, char *argv[]){

    string data[1000];
    int i,j;
    ifstream fin("input233.txt");
    if (!fin){
        cout << "Error opening file\n";
        return 0;
    }
    int count = 0;
    while(getline(fin,data[count])){
        count++;
    }
    if (argc == 1){
       int i = 0;
       int j = count - 1;
       while (i < j){
          string temp;
          temp = data[i];
          data[i] = data[j];
          data[j] = temp;
          i++;
          j--;
       }
       for (int i = 0; i<count; i++)
           cout << data[i] << endl;
    }
    else if (argc == 2){
        i = 0;
        j = count - 1;
        while (i < j){
          string temp;
          temp = data[i];
          data[i] = data[j];
          data[j] = temp;
          i++;
          j--;
        }
        if (strcmp("-c",argv[1])== 0){
           for (int i = 0; i< count; i++){
               int m = 0;
               int n = data[i].length()-1;
               while(m<n){
                  char temp = data[i][m];
                  data[i][m] = data[i][n];
                  data[i][n] = temp;
                  m++;
                  n--;
               }
               cout << data[i] << endl;
           }     
        }
        if (strcmp("-r",argv[1]) == 0){

           for (int i = 0; i<count; i++){
               string list[20];
               stringstream ss(data[i]);
               int count1 = 0;
               while(ss >> list[count1])
                    count1++;
               int r = 0;
               int s = count1-1;
               while(r < s){
                  string temp1 = list[r];
                  list[r] = list[s];
                  list[s] = temp1;
                  r++;
                  s--;
               }
               for (int w = 0; w<count1; w++)
                   cout << list[w] << " ";
               cout << endl;              
           }
        }     
    }
    return 0;
}

This had me check out if such an app has been written before in C. This lead me to the linux and other OSes "tac" command.

The source code weighs in at about 700 lines so I won't post it here but just a link.
https://github.com/coreutils/coreutils/blob/master/src/tac.c

No conversion required at all. Use the source Dawit.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.