TIP: Click on subject to list as thread! ANSI
echo: c_echo
to: F. Tourigny
from: George White
date: 1998-07-27 08:52:04
subject: K&R 1-9&12

Hi Frederic,

You asked:

FT> Is there a way to optimize this code?

I can make it longer by correcting a bug (see below).
Otherwise it looks like a valid solution within what has been presented
to that point in K&R.

FT> /*
FT> ** K&R Exercise 1-9
FT> ** Write a program to copy its input to its output,
FT> ** replacing each string of one or more blanks by
FT> ** a single blank.
FT> */

Exercise 1-7 in the first edition.

FT> #include 

FT> int main(void)
FT> {
FT>        int c;

FT>        while ((c = getchar()) != EOF) {
FT>               if (c != ' ')
FT>                      putchar(c);
FT>               else {
FT>                      printf(" ");
FT>                      while ((c = getchar()) == ' ')
FT>                             ;

/* You need to add these lines to handle the things when there is */
/* a space as the last character in a file */
                         if (EOF == c)
                            break;

FT>                      putchar(c);
FT>               }
FT>        }
FT>        return 0;
FT> }

FT> Or this?

FT> /*
FT> ** K&R Exercise 1-12
FT> **
FT> ** Write a program that prints its input one word per line.
FT> */

Not in the 1st edition.

You need to make a similar modification to the one above for the same
reason.

FT> #include 

FT> int main(void)
FT> {
FT>       int c;

FT>       while ((c = getchar()) != EOF) {
FT>             if (c == ' ') {
FT>                   while ((c = getchar()) == ' ')
FT>                         ;

Here.

FT>                   printf("\n");
FT>                   putchar(c);
FT>             }
FT>             else if (c == '\n') {
FT>                   while ((c = getchar()) == '\n')
FT>                         ;

and here.

FT>                   printf("\n");
FT>                   putchar(c);
FT>             }
FT>             else if (c == '\t') {
FT>                   while ((c = getchar()) == '\t')
FT>                         ;

and here.

FT>                   printf("\n");
FT>                   putchar(c);
FT>             }
FT>             else
FT>                   putchar(c);
FT>       }
FT>       return 0;
FT> }

FT> In the latter case, words are correctly printed out
FT> but aligning them all neatly seems to be beyond
FT> my reach.

They should all be left justified. The techniques needed to align the
ends of the words (right justify) is again beyond concepts presented to
that point in K&R.

George

 * SLMR 2.1a * Computers eliminate spare time.

--- Maximus/2 3.01
* Origin: DoNoR/2,Woking UK (44-1483-717904) (2:440/4)
SEEN-BY: 396/1 622/419 632/371 633/260 267 270 371 634/397 635/506 728
SEEN-BY: 639/252 670/213 218
@PATH: 440/4 255/1 252/356 140/1 270/101 396/1 633/260 635/506 728 633/267

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.