K&R2, Exercise 1-12

Discussion in 'C Programming' started by Josh Zenker, Sep 21, 2006.

  1. Josh Zenker

    Josh Zenker Guest

    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
    Josh Zenker, Sep 21, 2006
    #1
    1. Advertising

  2. Josh Zenker

    Michael Mair Guest

    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.
    Michael Mair, Sep 21, 2006
    #2
    1. Advertising

  3. Josh Zenker

    Old Wolf Guest

    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".
    Old Wolf, Sep 21, 2006
    #3
  4. 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.
    Spiros Bousbouras, Sep 22, 2006
    #4
  5. Josh Zenker

    Josh Zenker Guest

    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
    Josh Zenker, Sep 25, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kevin Spencer
    Replies:
    2
    Views:
    429
    John Saunders
    Aug 6, 2003
  2. lonelyplanet999

    Exercise needed for java 2 programmer test

    lonelyplanet999, Sep 30, 2003, in forum: Java
    Replies:
    1
    Views:
    4,171
    VisionSet
    Sep 30, 2003
  3. Xah Lee
    Replies:
    12
    Views:
    604
    Duncan Booth
    Jun 22, 2005
  4. Bruce .J Sam
    Replies:
    0
    Views:
    1,930
    Bruce .J Sam
    Jun 16, 2005
  5. Aries
    Replies:
    7
    Views:
    400
    Aries
    May 3, 2006
Loading...

Share This Page