6.4. Exercises
See Appendix A for answers.
Write a program that acts like cat, but reverses the order of the lines of all the lines from all the files specified on the command line or all the lines from standard input if no files are specified. (Some systems have a utility like this named tac.)
Modify the program from the previous exercise so that each file specified on the command line has its lines individually reversed. (Yes, you can do this with only what's been shown to you so far, even excluding the stroll in Chapter 1.)
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, not a 21-character column. That's a common mistake.)
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, Second Edition 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.