473,320 Members | 2,133 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Exercise 1-10 in K&R2

This is my attempt at exercise 1-10 in K&R2. The code looks sloppy to
me. Is there a more elegant way to do this?

#include <stdio.h>

/* copies input to output, printing */
/* series of blanks as a single one */
int main() {
int c;

while ((c = getchar()) != EOF) {
putchar(c);
if (c == ' ') {
while ((c = getchar()) == ' ')
; /* null statement */
putchar(c);
}
}

return 0;
}

It produces the expected output when compiled with gcc-3.4.6 on a
Gentoo box. So I'm assuming the code is at least correct, though not
necessarily optimal.

JZ

Sep 21 '06 #1
16 2227
Josh Zenker wrote:
This is my attempt at exercise 1-10 in K&R2. The code looks sloppy to
me. Is there a more elegant way to do this?

#include <stdio.h>

/* copies input to output, printing */
/* series of blanks as a single one */
int main() {
int c;

while ((c = getchar()) != EOF) {
putchar(c);
if (c == ' ') {
while ((c = getchar()) == ' ')
; /* null statement */
putchar(c);
Doesn't this print an extra space? You printed the character before the if.

--
Ian Collins.
Sep 21 '06 #2
Ian Collins wrote:
Josh Zenker wrote:
This is my attempt at exercise 1-10 in K&R2.
Oops, I meant exercise 1-9. I also realized after I posted this that
there's probably a ton of code out there for this exact exercise. I'll
search the group archives.
The code looks sloppy to
me. Is there a more elegant way to do this?

#include <stdio.h>

/* copies input to output, printing */
/* series of blanks as a single one */
int main() {
int c;

while ((c = getchar()) != EOF) {
putchar(c);
if (c == ' ') {
while ((c = getchar()) == ' ')
; /* null statement */
putchar(c);

Doesn't this print an extra space? You printed the character before the if.
I tested it on several different inputs, and it seems to produce the
desired output. For example, input "argle bargle" produces output
"argle bargle".

JZ

Sep 21 '06 #3
Josh Zenker wrote:
Ian Collins wrote:
>>Josh Zenker wrote:
>>>This is my attempt at exercise 1-10 in K&R2.


Oops, I meant exercise 1-9. I also realized after I posted this that
there's probably a ton of code out there for this exact exercise. I'll
search the group archives.

>>>The code looks sloppy to
me. Is there a more elegant way to do this?

#include <stdio.h>

/* copies input to output, printing */
/* series of blanks as a single one */
int main() {
int c;

while ((c = getchar()) != EOF) {
putchar(c);
if (c == ' ') {
while ((c = getchar()) == ' ')
; /* null statement */
putchar(c);

Doesn't this print an extra space? You printed the character before the if.


I tested it on several different inputs, and it seems to produce the
desired output. For example, input "argle bargle" produces output
"argle bargle".
Silly me, I didn't spot the while terminating with c != ' '.

--
Ian Collins.
Sep 21 '06 #4
Josh Zenker wrote:
This is my attempt at exercise 1-10 in K&R2. The code looks sloppy to
me. Is there a more elegant way to do this?

#include <stdio.h>

/* copies input to output, printing */
/* series of blanks as a single one */
int main() {
int c;

while ((c = getchar()) != EOF) {
putchar(c);
if (c == ' ') {
while ((c = getchar()) == ' ')
You need to check for EOF here.
; /* null statement */
putchar(c);
}
}

return 0;
}

It produces the expected output when compiled with gcc-3.4.6 on a
Gentoo box. So I'm assuming the code is at least correct, though not
necessarily optimal.
If you make the above correction then it's fine.

Sep 21 '06 #5
Josh Zenker wrote:
This is my attempt at exercise 1-10 in K&R2. The code looks sloppy to
me. Is there a more elegant way to do this?

#include <stdio.h>

/* copies input to output, printing */
/* series of blanks as a single one */
int main() {
int c;

while ((c = getchar()) != EOF) {
putchar(c);
if (c == ' ') {
while ((c = getchar()) == ' ')
Josh Zenker wrote:
This is my attempt at exercise 1-10 in K&R2. The code looks sloppy to
me. Is there a more elegant way to do this?

#include <stdio.h>

/* copies input to output, printing */
/* series of blanks as a single one */
int main() {
int c;

while ((c = getchar()) != EOF) {
putchar(c);
if (c == ' ') {
while ((c = getchar()) == ' ')
You have missed the EOF check here (as somebody has pointed out
already).
That is why it is a good idea to have one point input for any text
processing. Consider the following.

#include <stdio.h>

int main(void)
{
int c;
int blank = 0;
while((c = getchar()) != EOF)
{
if(c == ' ')
{
if(!blank)
{
blank = 1;
putchar(c);
}
}
else
{
blank = 0;
putchar(c);
}
}
return 0;
}

Sep 21 '06 #6
Josh Zenker wrote:
>
This is my attempt at exercise 1-10 in K&R2. The code looks
sloppy to me. Is there a more elegant way to do this?

#include <stdio.h>

/* copies input to output, printing */
/* series of blanks as a single one */
int main() {
int c;

while ((c = getchar()) != EOF) {
putchar(c);
if (c == ' ') {
while ((c = getchar()) == ' ')
; /* null statement */
putchar(c);
}
}

return 0;
}

It produces the expected output when compiled with gcc-3.4.6 on a
Gentoo box. So I'm assuming the code is at least correct, though
not necessarily optimal.
I like:

#include stdio.h

int main(void) {
int ch, lastch = EOF;

while (EOF != (ch = getchar())) {
if ((' ' != ch) && (' ' != lastch)) putchar(ch);
lastch = ch;
}
return 0;
} /* untested */

--
"The most amazing achievement of the computer software industry
is its continuing cancellation of the steady and staggering
gains made by the computer hardware industry..." - Petroski
--
Posted via a free Usenet account from http://www.teranews.com

Sep 21 '06 #7

Josh Zenker wrote:
This is my attempt at exercise 1-10 in K&R2. The code looks sloppy to
me. Is there a more elegant way to do this?
You can do this two general ways

(1) Note the state of "multiple spaces" by using a variable.

(2) Note it by your place in the code.

The main drawback to (2) is duplicated loops, which increases the
likelyhood you forget to do every check that is needed in every loop.
Hey! That happened, you're not checking for EOF while getting blanks!
You'll end up printing EOF as a character!

So I'd recommend method (1). Then you'll have just one place to do a
getchar, check for EOF, do a putchar. Hey! there's a problem there
too! You're not checking for error on putchar(). Don't feel bad, not
checking for errors has become rife. See any Microsoft example code.

Sep 21 '06 #8
On Thu, 21 Sep 2006, CBFalconer wrote:

[snip]
I like:

#include stdio.h

int main(void) {
int ch, lastch = EOF;

while (EOF != (ch = getchar())) {
if ((' ' != ch) && (' ' != lastch)) putchar(ch);
lastch = ch;
}
return 0;
} /* untested */
I assume that your '<' and '>' keys are broken today. :-)

Tak-Shing
Sep 21 '06 #9
Tak-Shing Chan wrote:
On Thu, 21 Sep 2006, CBFalconer wrote:

[snip]
>I like:

#include stdio.h

int main(void) {
int ch, lastch = EOF;

while (EOF != (ch = getchar())) {
if ((' ' != ch) && (' ' != lastch)) putchar(ch);
lastch = ch;
}
return 0;
} /* untested */

I assume that your '<' and '>' keys are broken today. :-)
Huh? I don't understand.

--
Some informative links:
news:news.announce.newusers
http://www.geocities.com/nnqweb/
http://www.catb.org/~esr/faqs/smart-questions.html
http://www.caliburn.nl/topposting.html
http://www.netmeister.org/news/learn2quote.html

--
Posted via a free Usenet account from http://www.teranews.com

Sep 21 '06 #10
CBFalconer said:
Tak-Shing Chan wrote:
>On Thu, 21 Sep 2006, CBFalconer wrote:

[snip]
>>I like:

#include stdio.h
<SNIP>
>>
I assume that your '<' and '>' keys are broken today. :-)

Huh? I don't understand.
How about now? :-)

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Sep 21 '06 #11
CBFalconer wrote:
Josh Zenker wrote:
>This is my attempt at exercise 1-10 in K&R2. The code looks
sloppy to me. Is there a more elegant way to do this?

#include <stdio.h>

/* copies input to output, printing */
/* series of blanks as a single one */
int main() {
int c;

while ((c = getchar()) != EOF) {
putchar(c);
if (c == ' ') {
while ((c = getchar()) == ' ')
; /* null statement */
putchar(c);
}
}

return 0;
}

It produces the expected output when compiled with gcc-3.4.6 on a
Gentoo box. So I'm assuming the code is at least correct, though
not necessarily optimal.

I like:

#include stdio.h

int main(void) {
int ch, lastch = EOF;

while (EOF != (ch = getchar())) {
if ((' ' != ch) && (' ' != lastch)) putchar(ch);
lastch = ch;
}
return 0;
} /* untested */
Testing is good. How about..

#include <stdio.h>

int main(void) {
int ch, last = 0;
while ((ch = getchar()) != EOF) {
if (!(last == ' ' && ch == ' '))
putchar(ch);
last = ch;
}
return 0;
}
...or..

#include <stdio.h>

int main(void) {
int ch, last = 0;
while ((ch = getchar()) != EOF) {
if (last != ' ' || ch != ' ')
putchar(ch);
last = ch;
}
return 0;
}
--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Sep 21 '06 #12
Josh Zenker wrote:
This is my attempt at exercise 1-10 in K&R2. The code looks sloppy to
me. Is there a more elegant way to do this?

#include <stdio.h>

/* copies input to output, printing */
/* series of blanks as a single one */
int main() {
int c;

while ((c = getchar()) != EOF) {
putchar(c);
if (c == ' ') {
while ((c = getchar()) == ' ')
; /* null statement */
putchar(c);
}
}

return 0;
}

It produces the expected output when compiled with gcc-3.4.6 on a
Gentoo box. So I'm assuming the code is at least correct, though not
necessarily optimal.

JZ
Hi Josh. That's Exercise 1-9 in my book, on page 20.

#include <stdio.h>

int main(void) {
int ch, last = 0;
while ((ch = getchar()) != EOF) {
if (last != ' ' || ch != ' ')
putchar(ch);
last = ch;
}
return 0;
}

Elegance?

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Sep 21 '06 #13
Richard Heathfield wrote:
CBFalconer said:
>Tak-Shing Chan wrote:
>>On Thu, 21 Sep 2006, CBFalconer wrote:

[snip]

I like:

#include stdio.h
<SNIP>
>>>
I assume that your '<' and '>' keys are broken today. :-)

Huh? I don't understand.

How about now? :-)
AHA ! :-) Good thing I put in the 'untested' comment.

--
Some informative links:
news:news.announce.newusers
http://www.geocities.com/nnqweb/
http://www.catb.org/~esr/faqs/smart-questions.html
http://www.caliburn.nl/topposting.html
http://www.netmeister.org/news/learn2quote.html
--
Posted via a free Usenet account from http://www.teranews.com

Sep 22 '06 #14
So far we've had 3 versions of code performing the
task which is exercise 1-10 in K&R2:

VERSION 1

#include <stdio.h>

/* copies input to output, printing */
/* series of blanks as a single one */
int main() {
int c;

while ((c = getchar()) != EOF) {
putchar(c);
if (c == ' ') {
while ((c = getchar()) == ' ')
if (c == EOF) return 0 ;
putchar(c);
}
}
return 0;
}

VERSION 2

#include <stdio.h>

int main(void)
{
int c;
int blank = 0;
while((c = getchar()) != EOF)
{
if(c == ' ')
{
if(!blank)
{
blank = 1;
putchar(c);
}
}
else
{
blank = 0;
putchar(c);
}
}
return 0;
}

VERSION 3

#include <stdio.h>

int main(void) {
int ch, last = 0;
while ((ch = getchar()) != EOF) {
if (last != ' ' || ch != ' ')
putchar(ch);
last = ch;
}
return 0;
}

Judging from some of the statements made here some
people consider versions 2 and 3 preferable on the
basis that they are less prone to bugs than version
1 which did have a bug in its original inception.

Personally I prefer version 1 because versions 2 and
3 have an extra assignment at every iteration of the
loop. On some platforms this *will* make a difference
in performance. Even if it won't or the difference in
performance turns out to be miniscule (and that will almost
certainly turn out to be the case on every realistic
input) it still does not agree at all with my sense of
aesthetics to introduce extra instructions in such a
blatant manner.

Apart from that I actually consider version 3 the
harder to write and verify.

Sep 22 '06 #15
"Spiros Bousbouras" <sp****@gmail.comwrites:
So far we've had 3 versions of code performing the
task which is exercise 1-10 in K&R2:
VERSION 1
int main() {
int c;
while ((c = getchar()) != EOF) {
putchar(c);
if (c == ' ') {
while ((c = getchar()) == ' ') if (c == EOF) return 0;
putchar(c);
}
}
return 0;
}
VERSION 2
int main(void) {
int c, blank = 0;
while((c = getchar()) != EOF) {
if(c == ' ') {
if(!blank) {
blank = 1;
putchar(c);
}
} else {
blank = 0;
putchar(c);
}
}
return 0;
}
VERSION 3
int main(void) {
int ch, last = 0;
while ((ch = getchar()) != EOF) {
if (last != ' ' || ch != ' ')
putchar(ch);
last = ch;
}
return 0;
}
[cut]
Personally I prefer version 1 because versions 2 and
3 have an extra assignment at every iteration of the
loop. On some platforms this *will* make a difference
in performance.
There are only two integers which both will probably be stored in
registers so a single "mov reg, reg" instruction won't make a big
difference IMO.
Even if it won't or the difference in
performance turns out to be miniscule (and that will almost
certainly turn out to be the case on every realistic
input) it still does not agree at all with my sense of
aesthetics to introduce extra instructions in such a
blatant manner.

Apart from that I actually consider version 3 the
harder to write and verify.
IMO the 3rd version is the easiest to write and understand. It's
straightforward, ie. the exercise is about printing all characters
unless it is a space and the previous character was a space, so if the
previous character (last != ' ') was not a space or this character is
not a space (ch != ' ') we shall print the character.

Moreover, there is only one way the loop may stop (ie. the loop
condition) and the first version involves return statement to stop the
loop which some prefer to avoid (just like break statement).

Only I would do:

#v+
#include <stdio.h>
int main(void) {
int ch, last = 0;
while ((ch = getchar()) != EOF) {
if (last != ' ' || ch != ' ') {
putchar(last = ch);
}
}
return 0;
}
#v-
--
Best regards, _ _
.o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o
..o | Computer Science, Michal "mina86" Nazarewicz (o o)
ooo +--<mina86*tlen.pl>--<jid:mina86*jabber.org>--ooO--(_)--Ooo--
Sep 22 '06 #16
On Thu, 21 Sep 2006 20:48:18 -0400, CBFalconer <cb********@yahoo.com>
wrote:
>Richard Heathfield wrote:
>CBFalconer said:
>>Tak-Shing Chan wrote:
On Thu, 21 Sep 2006, CBFalconer wrote:

[snip]

I like:
>
#include stdio.h
>
<SNIP>
>>>>
I assume that your '<' and '>' keys are broken today. :-)

Huh? I don't understand.

How about now? :-)

AHA ! :-) Good thing I put in the 'untested' comment.
Perhaps in the future you should change "untested" to "uncompiled" or
"uncompiled and untested" in your comments. Not compiling
code--without errors--is akin to and arguably worse than top-posting.

--
jay
Sep 23 '06 #17

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

41
by: Xah Lee | last post by:
here's another interesting algorithmic exercise, again from part of a larger program in the previous series. Here's the original Perl documentation: =pod merge($pairings) takes a list of...
11
by: Michael | last post by:
Hi, As an exercise, I created a class C that duplicates a value x thus having x_val1 and x_val2 I also overrided the + operator so that if I have z = x + y (x,y and z belong class C) then + ...
156
by: jacob navia | last post by:
There is a C tutorial at http://www.cs.virginia.edu/~lcc-win32 It is written to go with the compiler, available at the same URL. I have added quite a bit of material, and I would be glad if...
18
by: Andy Green | last post by:
Emphasis is on efficiancy and speed this is the excercise: The program should monitor a possibly infinite stream of characters from the keyboard (standard input). If it detects the sequence "aaa"...
20
by: Sushil | last post by:
Hi gurus I was reading FAQ "alloca cannot be written portably, and is difficult to implement on machines without a conventional stack." I understand that the standard does not mandate...
17
by: G Fernandes | last post by:
Can someone explain what is meant by "directory order" in the questoin for K and R 2 exercise 5-16? I can't seem to find a solution for this exercise on the main site where clc goers have posted...
16
by: arnuld | last post by:
i did what i could do at Best to solve this exercise and this i what i have come up with: ----------- PROGRAMME -------------- /* Stroustrup 5.9, exercise 3 STATEMENT: Use typedef to define...
14
by: arnuld | last post by:
there is no "compile-time error". after i enter input and hit ENTER i get a run-time error. here is the code: ---------- PROGRAMME -------------- /* Stroustrup, 5.9, exercise 11 STATEMENT:...
27
by: arnuld | last post by:
it works fine without any trouble. i want to have advice on improving the code from any angle like readability, maintenance etc: ---------- PROGRAMME ------------ /* Stroustrup, 5.9, exercise 11...
63
by: Bill Cunningham | last post by:
I don't think I can do this without some help or hints. Here is the code I have. #include <stdio.h> #include <stdlib.h> double input(double input) { int count=0,div=0; double...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.