Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > K&R2, Exercise 1-12

Reply
Thread Tools

K&R2, Exercise 1-12

 
 
Josh Zenker
Guest
Posts: n/a
 
      09-21-2006
I've written a solution based on Richard Heathfield's
(http://users.powernet.co.uk/eton/kandr2/krx112.html), but I'm
encountering some unexpected program behavior. Here's my code:

#include <stdio.h>

#define IN 1 /* inside a space */
#define OUT 0 /* outside a space */

/* prints input one word per line */
int main() {
int c, state;

state = OUT;
while ((c = getchar()) != EOF) {
if (c == ' ' || c == '\n' || c == '\t') {
if (state == OUT) {
state = IN;
putchar('\n');
}
/* else print nothing */
} else {
state = OUT;
putchar(c);
}
}

return 0;
}

I compiled with gcc-3.4.6 on Gentoo. When I run the executable, it
works fine for inputs like "foo bar" but gets fouled up when the input
contains a newline. For example, I typed "foo" followed by Ctrl+V and
Enter, followed by "bar" (i.e. "foo^Mbar") and got only "bar" as the
output. What's going on here?

JZ

 
Reply With Quote
 
 
 
 
Michael Mair
Guest
Posts: n/a
 
      09-21-2006
Josh Zenker schrieb:
> I've written a solution based on Richard Heathfield's
> (http://users.powernet.co.uk/eton/kandr2/krx112.html), but I'm
> encountering some unexpected program behavior. Here's my code:
>
> #include <stdio.h>
>
> #define IN 1 /* inside a space */
> #define OUT 0 /* outside a space */


If you just want different values, enumeration constants IMO are
a better choice.

>
> /* prints input one word per line */
> int main()


int main (void)
is the recommended form around here.

> {
> int c, state;
>
> state = OUT;
> while ((c = getchar()) != EOF) {
> if (c == ' ' || c == '\n' || c == '\t') {
> if (state == OUT) {
> state = IN;
> putchar('\n');
> }
> /* else print nothing */
> } else {
> state = OUT;
> putchar(c);
> }
> }
>
> return 0;
> }
>
> I compiled with gcc-3.4.6 on Gentoo. When I run the executable, it
> works fine for inputs like "foo bar" but gets fouled up when the input
> contains a newline. For example, I typed "foo" followed by Ctrl+V and
> Enter, followed by "bar" (i.e. "foo^Mbar") and got only "bar" as the
> output. What's going on here?


Are you sure that this input gives you what you expect?
Change your output to
printf("\\n[%d]\n", '\n');
and
printf("%c[%d]\n", c, c);
respectively to see what is going on.

If you can, redirect the input from a file to test your
programme -- then it should work as expected.

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
 
Reply With Quote
 
 
 
 
Old Wolf
Guest
Posts: n/a
 
      09-21-2006
Josh Zenker wrote:
> I've written a solution based on Richard Heathfield's
> (http://users.powernet.co.uk/eton/kandr2/krx112.html), but I'm
> encountering some unexpected program behavior. Here's my code:
>
> #include <stdio.h>
>
> #define IN 1 /* inside a space */
> #define OUT 0 /* outside a space */
>
> /* prints input one word per line */
> int main() {
> int c, state;
>
> state = OUT;
> while ((c = getchar()) != EOF) {
> if (c == ' ' || c == '\n' || c == '\t') {
> if (state == OUT) {
> state = IN;
> putchar('\n');
> }
> /* else print nothing */
> } else {
> state = OUT;
> putchar(c);
> }
> }
>
> return 0;
> }
>
> I compiled with gcc-3.4.6 on Gentoo. When I run the executable, it
> works fine for inputs like "foo bar" but gets fouled up when the input
> contains a newline. For example, I typed "foo" followed by Ctrl+V and
> Enter, followed by "bar" (i.e. "foo^Mbar") and got only "bar" as the
> output. What's going on here?


In ASCII, ^M is a carriage return. ^J is newline.

Probably your terminal treats it as such, i.e. it returns the cursor
to the start of the line but does not go down a line.

So your program outputs foo, and then goes back to the start of
the same line, and overwrites foo with bar.

Try typing "foot^Mbar", you will probably see "bart".

 
Reply With Quote
 
Spiros Bousbouras
Guest
Posts: n/a
 
      09-22-2006
Old Wolf wrote:

> Josh Zenker wrote:
> > I've written a solution based on Richard Heathfield's
> > (http://users.powernet.co.uk/eton/kandr2/krx112.html), but I'm
> > encountering some unexpected program behavior. Here's my code:
> >
> > #include <stdio.h>
> >
> > #define IN 1 /* inside a space */
> > #define OUT 0 /* outside a space */
> >
> > /* prints input one word per line */
> > int main() {
> > int c, state;
> >
> > state = OUT;
> > while ((c = getchar()) != EOF) {
> > if (c == ' ' || c == '\n' || c == '\t') {
> > if (state == OUT) {
> > state = IN;
> > putchar('\n');
> > }
> > /* else print nothing */
> > } else {
> > state = OUT;
> > putchar(c);
> > }
> > }
> >
> > return 0;
> > }
> >
> > I compiled with gcc-3.4.6 on Gentoo. When I run the executable, it
> > works fine for inputs like "foo bar" but gets fouled up when the input
> > contains a newline. For example, I typed "foo" followed by Ctrl+V and
> > Enter, followed by "bar" (i.e. "foo^Mbar") and got only "bar" as the
> > output. What's going on here?

>
> In ASCII, ^M is a carriage return. ^J is newline.
>
> Probably your terminal treats it as such, i.e. it returns the cursor
> to the start of the line but does not go down a line.
>
> So your program outputs foo, and then goes back to the start of
> the same line, and overwrites foo with bar.
>
> Try typing "foot^Mbar", you will probably see "bart".


Yes , this must be it. To be absolutely certain you
can pipe the output of the programme to od.

A more advanced version of your programme may
contain some special logic to handle control characters.

 
Reply With Quote
 
Josh Zenker
Guest
Posts: n/a
 
      09-25-2006
Old Wolf wrote:
> Josh Zenker wrote:
> > I've written a solution based on Richard Heathfield's
> > (http://users.powernet.co.uk/eton/kandr2/krx112.html), but I'm
> > encountering some unexpected program behavior. Here's my code:
> >
> > #include <stdio.h>
> >
> > #define IN 1 /* inside a space */
> > #define OUT 0 /* outside a space */
> >
> > /* prints input one word per line */
> > int main() {
> > int c, state;
> >
> > state = OUT;
> > while ((c = getchar()) != EOF) {
> > if (c == ' ' || c == '\n' || c == '\t') {
> > if (state == OUT) {
> > state = IN;
> > putchar('\n');
> > }
> > /* else print nothing */
> > } else {
> > state = OUT;
> > putchar(c);
> > }
> > }
> >
> > return 0;
> > }
> >
> > I compiled with gcc-3.4.6 on Gentoo. When I run the executable, it
> > works fine for inputs like "foo bar" but gets fouled up when the input
> > contains a newline. For example, I typed "foo" followed by Ctrl+V and
> > Enter, followed by "bar" (i.e. "foo^Mbar") and got only "bar" as the
> > output. What's going on here?

>
> In ASCII, ^M is a carriage return. ^J is newline.
>
> Probably your terminal treats it as such, i.e. it returns the cursor
> to the start of the line but does not go down a line.
>
> So your program outputs foo, and then goes back to the start of
> the same line, and overwrites foo with bar.
>
> Try typing "foot^Mbar", you will probably see "bart".


Yes, you're absolutely right. Thanks for clearing that up.

JZ

 
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