K&R Exercise 1-20 - Need some clarification

青春壹個敷衍的年華 提交于 2020-01-01 04:37:07

问题


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?


回答1:


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)




回答2:


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.



来源:https://stackoverflow.com/questions/7178201/kr-exercise-1-20-need-some-clarification

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!