Search code examples
ckernighan-and-ritchie

K&R Exercise 1-20 - Need some clarification


I don't fully understand what the following exercise is asking:

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

Could someone clarify the bolded part?


Solution

  • This exercise is asking you to emulate the behavior of tabs by adding the correct amount of spaces, such that the output is still aligned on tab stops.

    For example :

    "hello\tworld"
    

    should become :

    "hello   world"
    

    (the tab has been replaced with three spaces), if tab stops are every 4 columns (ie. n = 4).

    Or to clarify by indicating where the tab stops are :

    hello   world
    ^   ^   ^   ^
    

    If tab stops are every 3 columns, then you should get :

    hello world
    ^  ^  ^  ^
    

    (the tab was replaced by only 1 space)