Exercises
See Appendix A for answers to the following exercises:
[7] Write a program that acts like cat, but reverses the order of the output lines. (Some systems have a utility like this named tac.) If you run yours as
./tac fred barney betty, the output should be all of file betty from last line to first, then barney and then fred, also from last line to first. (Be sure to use the./in your program’s invocation if you call it tac so that you don’t get the system’s utility instead!)[8] Write a program that asks the user to enter a list of strings on separate lines, printing each string in a right-justified, 20-character column. To be certain that the output is in the proper columns, print a “ruler line” of digits as well. (This is simply a debugging aid.) Make sure that you’re not using a 19-character column by mistake! For example, entering
hello,good-byeshould give output something like this:123456789012345678901234567890123456789012345678901234567890 hello good-bye[8] Modify the previous program to let the user choose the column width so that entering
30,hello,good-bye(on separate lines) would put the strings at the 30th column. (Hint: see the section in Chapter 2 about controlling variable interpolation.) For extra credit, make the ruler line longer when the selected width is larger.