Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Exercise 1-10 in K&R2

Reply
Thread Tools

Exercise 1-10 in K&R2

 
 
Josh Zenker
Guest
Posts: n/a
 
      09-21-2006
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

 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      09-21-2006
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.
 
Reply With Quote
 
 
 
 
Josh Zenker
Guest
Posts: n/a
 
      09-21-2006
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

 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      09-21-2006
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.
 
Reply With Quote
 
Spiros Bousbouras
Guest
Posts: n/a
 
      09-21-2006
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.

 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      09-21-2006
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

 
Reply With Quote
 
suresh
Guest
Posts: n/a
 
      09-21-2006
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;
}

 
Reply With Quote
 
Ancient_Hacker
Guest
Posts: n/a
 
      09-21-2006

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.

 
Reply With Quote
 
Tak-Shing Chan
Guest
Posts: n/a
 
      09-21-2006
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
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      09-21-2006
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
tree functions daily exercise: Range Xah Lee Java 12 06-22-2005 08:51 AM
Cisco Student VPN exercise problem : gen_unrfrag: fail to generate unreachable, unexpected args robert Cisco 0 06-02-2004 07:33 PM
2154 module 4 Exercise 2 Drew Brown MCSE 0 10-22-2003 02:47 AM
Exercise needed for java 2 programmer test lonelyplanet999 Java 1 09-30-2003 10:37 AM
Re: Development best practices and knowing when to exercise control over development Kevin Spencer ASP .Net 2 08-06-2003 09:33 PM



Advertisments