Re: Small Correction to K&R Exercise 1-22 Solution on CLC-Wiki
- From: Netocrat <netocrat@xxxxxxxxxxx>
- Date: Sun, 21 May 2006 05:42:08 GMT
[excuse the previous double-posting - a glitch in the initial posting
combined with my unfamiliarity with a new test version of the newsreader
that I use led me to believe that the initial post failed and I didn't
wait long enough to check for its non-appearance to confirm this]
On Sat, 20 May 2006 18:26:46 -0700, Mike S wrote:
Netocrat wrote:
Mike S wrote:
I noticed a very slight logic error in the solution to K&R Exercise
1-22 on the the CLC-Wiki, located at
http://www.clc-wiki.net/wiki/KR2_Exercise_1-22
My fix is now reflected at the above address.
Are you going to add your analysis too?
Your fix looks correct to me at least. How about you edit the code on
the wiki and add your analysis to the discussion page?
I've spotted another bug that I won't point out in case readers would
like to find it for themselves.
Your code answering the challenge includes the obvious fix for this bug -
partial confirmation that you have, as you wrote in your previous post,
found the same bug.
The exercise reads as follows:
"Write a program to 'fold' long input lines into two or more shorter
lines after the last non-blank character that occurs before the n-th
column of input. Make sure your program does something intelligent
with very long lines, and if there are no blanks or tabs before the
specified column."
Aside from that, I don't think that overwriting the n-th character with
a newline when a line contains no spaces before the n-th character
conforms to the spirit or the letter of the exercise.
So, a small challenge for c.l.c readers: what's the smallest possible
change that could be made to this code that would preserve all input
characters in the output for over-length words without breaking the
code's existing conformance to the exercise's wording?
To fully complete the challenge, the adjustment should retain the fix
suggested by Mike S as well as fix the other unidentified bug.
Here is my answer to the challenge, though it is only guaranteed to work
if the input is 7-bits-packed-in-8-bits ASCII, because I take advantage
of the sign bit in order to fit the notion of a printable char and a
line-break into one array location when no spaces occur in a line. The
reasoning behind this design choice was that I didn't want to go through
the trouble (and the extra code) of making room in the array for the
line break in this case, so I thought why not squeeze it into a single
char location by letting a set sign bit indicate that a line break is
needed?
Here's an insight that could be used to avoid both choices: nothing in the
exercise requires output to occur in one final step.
I also added a line or two to handle tabs correctly; it assumes a
tabstop of 8 (as the "de facto" tabstop, it's good enough for me...).
That change goes beyond the challenge, but it seems sensible. It would be
useful to make tab width configurable through a macro though.
[...(including code)...]
--
http://members.dodo.com.au/~netocrat
.
- References:
- Prev by Date: Re: math.h trig functions questions (and some forgotten high school math)
- Next by Date: Re: Use of diff pointers
- Previous by thread: Re: Small Correction to K&R Exercise 1-22 Solution on CLC-Wiki
- Next by thread: String Matching Problem (NUXI problem) ?
- Index(es):
Relevant Pages
|