Thread: K&R exercise

  1. #1
    Registered User
    Join Date
    Jul 2009
    Location
    Croatia
    Posts
    272

    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?

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    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.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with K&R Book Exercise
    By Alejandrito in forum C Programming
    Replies: 5
    Last Post: 03-11-2008, 01:24 PM
  2. Exercise 2-6 K&R help
    By allix in forum C Programming
    Replies: 19
    Last Post: 08-18-2006, 09:25 AM
  3. K&R Exercise 1-14
    By Lee134 in forum C Programming
    Replies: 3
    Last Post: 02-16-2006, 11:20 AM
  4. Help with K&R Exercise 1.13
    By Yannis in forum C Programming
    Replies: 2
    Last Post: 09-21-2003, 02:51 PM
  5. k&r exercise 1-9
    By xion in forum C Programming
    Replies: 14
    Last Post: 07-15-2003, 06:01 PM