Exercises

See Appendix A for answers.

  1. Write a program that acts like type, but reverses the order of the lines from the files specified on the command line, or all the lines from standard input if no files are specified.

  2. Modify the program from the previous exercise so that each file specified on the command line has its lines individually reversed. That is, the output consists of the last line of the first file through the first line of the first file, followed by the last line of the second file through the first line of the second file, until all the files are complete.

  3. Write a program that reads a list of strings on separate lines, and prints the strings in a right-justified 20-character column. For example, inputting hello, good-bye prints hello and good-bye, right-justified in a 20-character column. (Be sure your program is actually using a 20-character column, and not a 21-character column. That mistake is common.)

  4. Modify the program from the previous exercise to allow the user to select the column width. For example, entering 20, hello, and good-bye should do the same thing as the previous program did; but, entering 30, hello, and good-bye should justify hello and good-bye in a 30-character column.

Get Learning Perl on Win32 Systems now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.