I don't quite understand this exercise...

P

Patrick M.

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

Charlie Gordon

Patrick M. said:
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 ?
 
G

Gordon Burditt

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

Thad Smith

Patrick said:
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
 
I

Ian Malone

Gordon said:
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)
 
P

Patrick M.

Thad said:
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.
 
G

Gordon Burditt

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
 
P

Patrick M.

Gordon said:
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.
 
R

Roberto Waltman

Patrick M. said:
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. ]
 
M

Mark McIntyre

On Mon, 26 Sep 2005 19:46:41 -0000, in comp.lang.c ,
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 ?
 
K

Keith Thompson

Patrick M. said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top