I need help understanding what Exercise 5-12 is asking for in the C Programming Language book
-
27-09-2019 - |
Question
K&R C Programming Language: pg. 105
Extend
entab
anddetab
to accept the shorthand
entab -m +n
to mean tab stops every
n
columns, starting at columnm
.
entab
replaces a number of spaces with a tab character and detab
does the opposite. The question I have concerns the tab stops and entab
. I figure that for detab
it's pretty easy to determine the number of spaces needed to reach the next tab stop, so no worries there. With entab
, replacing spaces with tabs is slightly more difficult since I cannot for sure know how large the tab character goes to its own tab stop (unless there is a way to know for sure).
Am I even thinking about this thing properly?
Solution
"tab stops every n columns, starting at column m" tells you how large each tab stop is, at least by my reading: it's just n. Only the first tab stop is different; that one is m.
OTHER TIPS
entab needs to work out when runs of spaces reach a tabstop - then that run of spaces can be replaced by a tab character.
For example, the following line of text (the ruller is there for reference):
1 1 2 2 2
1 8 2 6 0 4 8
-------+---+---+---+---+---+
this is a line
should look like the following after entab -8 +4
:
\t this \tis a line
(note that it might be reasonable for there to be two tab characters following 'this' in the line, since either a space or a tab would reach that particular tab stop).