12

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?

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
  • Do a first version of the program for `n` being 8. Then another for `n` being 4. Than a final one for `n` specified on the command line or some other configuration method. – pmg Aug 24 '11 at 15:40

2 Answers2

23

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)

Sander De Dycker
  • 16,053
  • 1
  • 35
  • 40
  • Hmm, the first tab stop is usually not adjacent to the left margin: `_______^_______^_______^__...` – pmg Aug 24 '11 at 16:02
  • 3
    @pmg It doesn't hurt to think of the first tab stop on the left margin, because each tab inserts at least one space character, so you move past it anyway. I think it's clearer to include the tab stop at position 0 because it fits better into the pattern of the others. – sethobrien Aug 24 '11 at 16:19
  • @pmg : I admit it's not really useful (but not harmful either). I've removed them to avoid confusion - thanks for pointing it out. – Sander De Dycker Aug 24 '11 at 16:21
  • @sethobrien : that was my reasoning too. I guess I'll let the votes for both your comments decide whether I should put them back or not. I don't have a strong feeling either way. – Sander De Dycker Aug 24 '11 at 16:24
  • I'm convinced I was wrong. Tabs at column 0 are the way to go. sethobrien comment up voted :) – pmg Aug 24 '11 at 17:51
4

If you take an example of tabstops being set at n=8, for example, the if the input has 1 character, the tab will add 7 spaces (to bring you to column 9). Basically, don't always add n spaces, add the number of spaces that brings you to the appropriate column for your particular value of n.

For example:

         1
1234567890123456789
1------>
123---->
More words----->

"Assume a fixed set of tab stops" is for non-programmers, basically. We're used to a tab always aligning on a multiple of 4,8,etc. But in word processors, the tab stops are configurable... so the first tab would align you on column 6, the second would go to 30 (for example to sloppily center text) and the third would give you column 70 (for page numbers or something). He's just specifying here that we're talking about "programmer" tabstops, not a word processor's tabstops.

jkerian
  • 16,497
  • 3
  • 46
  • 59