-
K&R exercise
Write a program to "fold" long input lines into two or more shorter lines after the last non-blank character that occurs before the n -th column of input. Make sure your program does something intelligent with very long lines, and if there are no blanks or tabs before the specified column.
1. Is it better (practical?) to store a line into array and then work with it, or just go with while((c=getchar())!=EOF) ? Why?
2. What would be an intelligent thing to do with very long lines / and if there are no blanks before the nth column?
3. Example... input like:
Code:
#define LINE_WIDTH 10
char a[40] = "testing testing2";
Fold_line(a);
Should i ignore the blanks in between the 2 words, and put a \n right before testing2 word?
-
1) I would store into an array first, reading up to the newline. Then, work with that line.
2) Several choices here.
2a) simply truncate at the max col allowed and wrap.
2b) lop the word off at maxcol - 1, add a hypen, and continue the next line.
2c) lop the word off at maxcol - 1, add a "special character" (perhaps backslash), and continue next line.
2d) Determine the length of the line that won't fit, find an even divisor, say 5, and write out the line across 5 lines for length/5 character each line
2e) If the output is something like html, write a portion of the line and make it a hyperlink to a popup for the whole text
etc., etc., etc.
3) It's your exercise, try both and see how you like it.