I don't quite understand this exercise...

Discussion in 'C Programming' started by Patrick M., Sep 26, 2005.

  1. Patrick M.

    Patrick M. Guest

    I don't quite understand what K&R want me to do in Exercise 1-20 in "The
    C Programming Language, 2nd Edition". Here's the description:

    "Write a program detab that replaces tabs in the input with the proper
    number of blanks to space to the next tab stop. Assume a fixed set of
    tab stops, say every n columns. Should n be a variable or a symbolic
    parameter?"

    What does this mean? Do they want me to figure out the amount of spaces
    a tab takes up on my system, and add them? I'm sort of confused about
    this question.
     
    Patrick M., Sep 26, 2005
    #1
    1. Advertisements

  2. You can assume a fixed pitch font, but should you assume a fixed tab size ?
     
    Charlie Gordon, Sep 26, 2005
    #2
    1. Advertisements

  3. "Write a program detab that replaces tabs in the input with the proper
    Let me ask a question I think is related: should you have to
    recompile the program to change from assuming tab stops every 4
    columns to every 8 columns, or should this be a command-line
    parameter?

    Gordon L. Burditt
     
    Gordon Burditt, Sep 26, 2005
    #3
  4. Patrick M.

    Thad Smith Guest

    They mean: write a program to read a text file (or stdin) with tabs and
    replace all the tabs with equivalent space characters. The result
    should be an output file (or stdout). This is a general type of program
    known as a filter, which takes one input file and produces an associated
    output file with some defined modification.

    They are suggesting that you might want to allow some way to specify the
    tab spacing or tab columns that your input file assumes. That can be
    done with a command line parameter.

    Thad
     
    Thad Smith, Sep 26, 2005
    #4
  5. Patrick M.

    Ian Malone Guest

    Just to add one thing the OP may have missed (although I'll own
    up now to never having read K&R): tabs are typically used to
    indicate that the next character goes at the start of the
    'next'[1] 'tab column' i.e. they have variable width.

    [1] Inverted quotes because they typically leave a space, so the
    start of the 'next' column is not always the immediately
    following column.
    |
    ---> (think of the above as an example of resulting layout)
     
    Ian Malone, Sep 26, 2005
    #5
  6. Patrick M.

    Patrick M. Guest

    Ok, thanks, and everyone else. So what you're saying is, they want me to
    ask the user/get and set a variable, say n, and replace all tabs with n
    spaces, and they don't want me to calculate the number of spaces a tab
    takes up on the system?

    Thanks for all the replies.
     
    Patrick M., Sep 26, 2005
    #6
  7. Ok, thanks, and everyone else. So what you're saying is, they want me to
    How can you possibly calculate the number of spaces a tab takes up
    on the system? (Isn't this a little like calculating the color of
    the nail polish on THE thumb of THE human?) It could be that there
    are many different terminals and printers, all with different tab
    stop settings, and in any case, there's no portable way to read the
    location of the cursor after sending a tab.

    There are some conventions where a file is labelled with the number
    of spaces a tab should take up while editing it with vi, but that's
    hardly universal (and using it tends to be a security hole). I've
    seen only a very few files actually using such labelling. Some
    files have comments like "view me with 4 space tabs". It is often
    the case that even the files in the same directory are not consistent
    among themselves.

    Gordon L. Burditt
     
    Gordon Burditt, Sep 26, 2005
    #7
  8. Patrick M.

    Patrick M. Guest

    Yes, you do have a point there... :D But there could possibly be a way,
    I just wasn't sure if that's what they wanted me to do; you never know.
    Thanks for clearing it up for me.
     
    Patrick M., Sep 26, 2005
    #8
  9. Almost there... They are asking for a program that will take as input:

    (a) A text file containing some tab characters. And
    (b) A number 'N' telling what is the tab "spacing"

    And produce, as output, a text file where every tab has been replaced
    by a *variable* number of spaces between 1 and N so that the file
    "looks" similar to the original when displayed or printed in a device
    that uses 'N' colums per tab.

    For example, if N is 4 and the original file is:

    int i = 0;
    int jj = 0;
    int kkk = 0;

    Containing a tab before each 'int', a space after each 'int' and a tab
    before each '=', the output should be:

    int i = 0;
    int jj = 0;
    int kkk = 0;

    containing 4 spaces before each int, one space after each int, 3
    spaces after 'i', 2 spaces after 'jj' and 1 space after 'kkk';



    Roberto Waltman

    [ Please reply to the group, ]
    [ return address is invalid. ]
     
    Roberto Waltman, Sep 26, 2005
    #9
  10. On Mon, 26 Sep 2005 19:46:41 -0000, in comp.lang.c ,
    One assumes, dear boy, they mean the system the acolyte is sitting
    before, perhaps ?
     
    Mark McIntyre, Sep 26, 2005
    #10
  11. You can't simply "replace all tabs with n spaces". A tab doesn't
    expand to a fixed number of spaces; it expands to one or more spaces,
    however many are needed to reach the next tab stop. This will vary
    depending on where in the line the tab happens to appear.
     
    Keith Thompson, Sep 26, 2005
    #11
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.