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.
    /* EOF */
    Patrick M., Sep 26, 2005
    #1
    1. Advertising

  2. "Patrick M." <> wrote in message
    news:...
    > 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.


    You can assume a fixed pitch font, but should you assume a fixed tab size ?

    --
    Chqrlie.
    Charlie Gordon, Sep 26, 2005
    #2
    1. Advertising

  3. >"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.


    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

    Patrick M. wrote:

    > 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.


    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

    Gordon Burditt wrote:
    >>"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.

    >
    >
    > 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?
    >


    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)

    --
    imalone
    Ian Malone, Sep 26, 2005
    #5
  6. Patrick M.

    Patrick M. Guest

    Thad Smith wrote:
    > Patrick M. wrote:
    >
    >> 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.

    >
    > 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
    >


    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.
    /* EOF */
    Patrick M., Sep 26, 2005
    #6
  7. >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?


    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

    Gordon Burditt wrote:
    >> 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?

    >
    > 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


    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.
    /* EOF */
    Patrick M., Sep 26, 2005
    #8
  9. "Patrick M." wrote:
    >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?
    >

    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 ,
    (Gordon Burditt) wrote:

    >How can you possibly calculate the number of spaces a tab takes up
    >on the system?


    One assumes, dear boy, they mean the system the acolyte is sitting
    before, perhaps ?

    --
    Mark McIntyre
    CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
    CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

    ----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
    http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
    ----= East and West-Coast Server Farms - Total Privacy via Encryption =----
    Mark McIntyre, Sep 26, 2005
    #10
  11. "Patrick M." <> writes:
    [...]
    > 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?


    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 (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Sep 26, 2005
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. OscarC
    Replies:
    7
    Views:
    861
    Roedy Green
    May 9, 2004
  2. Suzie
    Replies:
    22
    Views:
    890
    Peter =?ISO-8859-15?Q?K=F6hlmann?=
    Oct 30, 2004
  3. Dan
    Replies:
    5
    Views:
    537
    Bikal KC
    Aug 23, 2007
  4. gwellman
    Replies:
    3
    Views:
    148
    gwellman
    Dec 4, 2003
  5. Jedrin
    Replies:
    1
    Views:
    519
    sreservoir
    Feb 18, 2012
Loading...

Share This Page