Lew Pitcher <> writes:
> On November 7, 2010 14:00, in comp.lang.c, wrote:
>
>> On Nov 7, 6:32Â*am, Eric Sosman <esos...@ieee-dot-org.invalid> wrote:
>>
>>> Side-note, this program is considerably more complicated (and
>>> self-limited) than it needs to be to accomplish its stated task.
>>> As you learn more, you'll see ways to simplify and improve it --
>>> but right now, let's focus on the basics.
>>>
>>
>> I don't think this is what the op was looking for, but like, here is
>> what I came up with...
>
> Ditto....
>
> ----------------------------cut------------------------------
>
> /*
> ** capit.c - capitalize first letter of each input line
> **
> ** Input: stdin
> ** Process: capitalize the first alphabetic character of
> ** each input line.
> **
> ** If ALLOWLEADINGBLANKS is defined, only capitalize
> ** if letter is preceeded by zero or more whitespace;
> ** *DO NOT* capitalize if first letter is preceeded
> ** by non-whitespace.
> ** Output: stdout
> */
>
> #include <stdio.h>
> #include <stdlib.h>
> #include <ctype.h>
>
> #define ALLOWLEADINGBLANKS 1
>
> void capit(FILE *input, FILE *output)
> {
> int datum, linestart;
>
> for (linestart = 1,datum = getc(input);datum != EOF;datum = getc(input))
> {
> if (linestart)
> {
> #ifdef ALLOWLEADINGBLANKS
> for (;isblank(datum); datum = getc(input)) putc(datum,output);
> #endif
> if (islower(datum)) datum = toupper(datum);
> }
> putc(datum,output);
> linestart = (datum == '\n' ? 1 : 0);
> }
> }
The ALLOWLEADINGBLANKS version will go wrong when the input ends after
an initial blank. It's possible that such an input (as a text file) is
considered to trigger undefined behaviour, but prefer to handle them
since they are no rare "in the wild".
I would not normally comment on style but, since I'm posting, I'll
mention that I find
for (linestart = 1,datum = getc(input);datum != EOF;datum = getc(input))
a little odd. For one thing, linestart has nothing to do with the loop,
so I's initialise it to 1 in the declaration rather than here but, more
significantly,
while ((datum = getc(input)) != EOF)
is such a widely-know idiom, it took me while to grok yours as being the
same loop!
> int main(void)
> {
> capit(stdin,stdout);
> return EXIT_SUCCESS;
> }
<snip>
--
Ben.