Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.2 9/18/84; site diablo.ARPA Path: utzoo!watmath!clyde!burl!ulysses!allegra!mit-eddie!genrad!decvax!decwrl!glacier!diablo!avg From: avg@diablo.ARPA Newsgroups: net.lang Subject: Re: Comments on this program please... Message-ID: <105@diablo.ARPA> Date: Wed, 4-Dec-85 22:57:33 EST Article-I.D.: diablo.105 Posted: Wed Dec 4 22:57:33 1985 Date-Received: Sat, 7-Dec-85 03:12:26 EST References: <2389@ukma.UUCP> <7839@ucla-cs.ARPA> Reply-To: avg@diablo.UUCP (Allen VanGelder) Organization: Stanford University Lines: 57 Summary: In article <7839@ucla-cs.ARPA> jimc@ucla-cs.UUCP (Jim Carter) commented on: >>#include >>main() >>{ >> register int r, o, u, t, e; >> while ((r = getchar()) != EOF) { >> if (r == 'R') >> if ((o = getchar()) == 'O') >> if ((u = getchar()) == 'U') >> if ((t = getchar()) == 'T') >> if ((e = getchar()) == 'E') >> printf("\nROUTE"); >> else >> printf("ROUT%c", e); >> else >> printf("ROU%c", t); >> else >> printf("RO%c", u); >> else >> printf("R%c", o); >> else >> putchar(r); >> } >> putchar('\n'); >>} and submitted his own "improvement". My question is, "What is the original program supposed to do?" Judging from Jim's comments, he believes that input ROUROUTE should produce output ROU\nROUTE\n. It doesn't. However, input ROUXROUTE produces ROUX\nROUTE\n. So perhaps the answer to "What's wrong with this program?" is "It doesn't meet specs." On a very simple style level, what's wrong is that it uses nested if's when it could use if ... else if ... else if ... The following version corrects both complaints. The technique of getting the first char before the loop is called "priming", and has wide applicability. #include main() { register int r, o, u, t, e; r = getchar(); while (r != EOF) { if (r != 'R') { putchar(r); r = getchar(); } else if ((o = getchar()) != 'O') { printf("R"); r = o; } else if ((u = getchar()) != 'U') { printf("RO"); r = u; } else if ((t = getchar()) != 'T') { printf("ROU"); r = t; } else if ((e = getchar()) != 'E') { printf("ROUT"); r = e; } else { printf("\nROUTE"); r = getchar(); } } putchar('\n'); } Not to pick on Jim C., but I don't see how his program, or any other program that is mainly explanatory comments, can be considered "cute."